In Scott Meyer's new book, he proposes an example usage for rvalue reference qualifiers that looks something like this:
class Widget {
private:
DataType values;
public:
DataType& data() & { return values; }
DataType data() && { return std::move(values); } // why DataType?
};
So that:
auto values = makeWidget().data();
move-constructs values
instead of copy-constructing it.
Why does the rvalue-ref-qualified data()
return DataType
instead of DataType&&
? auto
would still deduce DataType
in that case (although decltype(auto)
wouldn't - but that can't be the only reason to prefer to return a value instead of an ravlue ref). This highly voted answer returns an rvalue ref, which makes more sense conceptually to me.