Name | Ownership | Copyable | Movable | Sharing | Lifetime | Semantics |
---|---|---|---|---|---|---|
unique_ptr | Unique | ❌ | ✅ | ❌ | Lexical | Reference |
shared_ptr | Shared | ✅ | ✅ | Reference | Reference-counted | Reference |
weak_ptr | ❌ | ✅ | ✅ | ❌ | Non-extending | Optional-reference |
value_ptr | Unique | ✅ | ✅ | Value | Lexical | Value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main: # @main | |
mov eax, 16 | |
ret |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
constexpr int square(int x) { | |
return x * x; | |
} | |
int main() { | |
return square(4); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Foo::Pimpl { | |
int x; | |
int bar() { return ++x; } | |
}; | |
Foo::Foo(int x) | |
: ptr{Foo::Pimpl{x}} | |
{} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Foo { | |
int bar() { | |
return ptr->bar(); | |
} | |
Foo(int x); | |
// Thanks to value_ptr we get value semantics for free | |
Foo(Foo const&) = default; | |
Foo& operator=(Foo const&) = default; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Tree { | |
string const name; | |
value_ptr<Tree> left; | |
value_ptr<Tree> right; | |
Tree( | |
string const& name, | |
value_ptr<Tree> const& left = value_ptr<Tree>{}, | |
value_ptr<Tree> const& right = value_ptr<Tree>{}) | |
: name{name} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Node { | |
string name; | |
weak_ptr<Node> parent; | |
vector<shared_ptr<Node>> children; | |
bool hasParent() { | |
return parent.expired(); | |
} | |
shared_ptr<Node> getParent() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Widget { | |
Widget() { cout << "Widget created" << endl; } | |
~Widget() { cout << "Widget destroyed" << endl; } | |
}; | |
unique_ptr<Widget> createWindow() { | |
return make_unique<Widget>(); | |
} | |
int main() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void* x = createInstance(); | |
int* y = new int(); | |
foo.bar(y); |