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*

23
  • 6
    @Nicol: but std::move doesn't name its return value. Remember that named rvalue references are lvalues. ideone.com/VlEM3 Commented Nov 14, 2011 at 0:16
  • 39
    I basicaly agree with this answer, but have some remarks. (1) I don't think there is a valid use case for passing reference to const lvalue: everything the callee could do with that, it can do with reference to const (bare) pointer too, or even better the pointer itself [and it's none of its business to know ownership is held throught a unique_ptr; maybe some other callers need the same functionality but are holding a shared_ptr instead] (2) call by lvalue reference could be useful if called function modifies the pointer, e.g., adding or removing (list-owned) nodes from a linked list. Commented Jun 22, 2014 at 18:55
  • 10
    ...(3) Although your argument favoring passing by value over passing by rvalue reference makes sense, I think that the standard itself always passes unique_ptr values by rvalue reference (for instance when transforming them into shared_ptr). The rationale for that might be that it is slightly more efficient (no moving to temporary pointers is done) while it gives the exact same rights to the caller (may pass rvalues, or lvalues wrapped in std::move, but not naked lvalues). Commented Jun 22, 2014 at 18:58
  • 22
    Just to repeat what Marc said, and quoting Sutter: "Don’t use a const unique_ptr& as a parameter; use widget* instead" Commented Jul 29, 2014 at 12:16
  • 23
    We've discovered a problem with by-value -- the move takes place during argument initialization, which is unordered with respect to other argument evaluations (except in an initializer_list, of course). Whereas accepting an rvalue reference strongly orders the move to occur after the function call, and therefore after evaluation of other arguments. So accepting rvalue reference should be preferred whenever ownership will be taken. Commented Feb 5, 2015 at 5:00