Skip to content

Instantly share code, notes, and snippets.

View njlr's full-sized avatar
🌏
F# ing

njlr njlr

🌏
F# ing
View GitHub Profile
main: # @main
mov eax, 16
ret
constexpr int square(int x) {
return x * x;
}
int main() {
return square(4);
}
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
struct Foo::Pimpl {
int x;
int bar() { return ++x; }
};
Foo::Foo(int x)
: ptr{Foo::Pimpl{x}}
{}
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;
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}
struct Node {
string name;
weak_ptr<Node> parent;
vector<shared_ptr<Node>> children;
bool hasParent() {
return parent.expired();
}
shared_ptr<Node> getParent() {
struct Texture {
  Texture(string const& path) { cout << "Texture loaded from " << path << endl; }
 ~Texture() { cout<< "Texture destroyed" << endl; }
 
  static shared_ptr<Texture> load(string const& path) {
  return make_shared<Texture>(path)
  }
};
int doSomethingWithTexture(shared_ptr<Texture> const tex);
struct Widget {
 Widget() { cout << "Widget created" << endl; }
  ~Widget() { cout << "Widget destroyed" << endl; }
};
unique_ptr<Widget> createWindow() {
 return make_unique<Widget>();
}
int main() {
void* x = createInstance();
int* y = new int();
foo.bar(y);