unique_ptr private member

Started by
13 comments, last by SiCrane 11 years, 4 months ago
There's no point in adding the std::move() call. The unique_ptr is already an rvalue because it's a temporary.
Advertisement

There's no point in adding the std::move() call. The unique_ptr is already an rvalue because it's a temporary.


I'm myself still a bit unsure about all the details of rvalues, but wouldn't it be more correct to say "unnamed temporary" because not every temporary automatically is an rvalue?
No, every temporary is automatically an rvalue; it's part of the definition of rvalue. It's possible to bind a temporary to a named non-rvalue reference, but that named reference is itself not a temporary.
For the record, I think I found where I got myself confused from back where I read this. An rvalue reference can (easily) become an unexpected lvalue (because it has a name). In the following scenario the std::move is needed to use the move assignment operator:

void doSomething(X&& x)
{
X anotherX = std::move(x);
// whatever...
}
Of course in this case neither a temporary unique_ptr nor a rvalue reference to one needs to be used. std::vector::emplace_back() can be used to construct the object in place directly without bothering with a move.

mAtlasList.emplace_back(new Atlas(MASTER_ATLAS_MAINMENU));

This topic is closed to new replies.

Advertisement