Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

9
  • Then in such case one would have to implement a copy constructor? Commented Sep 26, 2016 at 13:07
  • @vordhosbn If that is necessary. Commented Sep 26, 2016 at 13:11
  • That's true but my question was about move construction/assignment. Using plain unique_ptr instead of const will keep move construction/assignment alive and well while declaring it const forbids it. I used to think that const data members is a bad practise (because of this issue) but apparently Herb Sutter doesn't think this. I wondered why. Commented Sep 26, 2016 at 13:15
  • 1
    I would argue the opposite to Mr Sutter. The default behaviour of std::move on std::unique_ptr is absolutely correct and very much what we want - an efficient transfer of state, leaving the moved-from handle in a very well defined state - "invalid, do not touch". Disabling moves explicitly is one thing (one might wonder at the motives). Disabling them through cryptic use of const just looks to me like playfulness. Commented Sep 26, 2016 at 16:33
  • 6
    To me, the most important thing is to express lifetime by construction, and to get the defaults right (in this case, no brittle move by default). You can write the copy and move operations yourself, e.g., MyClass& operator=(const MyClass& that) { *pimpl = *that.pimpl; return *this; }. The Impl class can still be copyable and even movable, and you can delegate to it. Of course that only gets you 95% of the way, and there's no objection to also writing your own value_ptr type that automates the deep copy/move too. Commented Sep 27, 2016 at 5:18