Skip to content

Instantly share code, notes, and snippets.

@njlr
Created May 25, 2017 13:49
Show Gist options
  • Save njlr/bbbcbc0ae50c796d8014ac7b0e42ebeb to your computer and use it in GitHub Desktop.
Save njlr/bbbcbc0ae50c796d8014ac7b0e42ebeb to your computer and use it in GitHub Desktop.
struct Widget {
 Widget() { cout << "Widget created" << endl; }
  ~Widget() { cout << "Widget destroyed" << endl; }
};
unique_ptr<Widget> createWindow() {
 return make_unique<Widget>();
}
int main() {
  auto w = createWindow();
 ASSERT(w);
  // auto w2 = widget; // error: unique_ptr has no copy-constructor
 auto w3 = move(widget); // w3 owns now the widget, w is empty
  ASSERT(w3 && !w);
 // w3 calls destructor of widget; only one object at a time owns the Widget
return 0;
}
@insanustu
Copy link

There is no variable 'widget'. So line 14 should be 'auto w3 = move(w)', and line 13 should contain 'auto w2 = w ...'

@apankrat
Copy link

apankrat commented May 26, 2017

- auto w3 = move(widget);
+ auto w3 = move(w);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment