Reputation: 2400
I am primarily trying to understand the uses that this feature enables or facilitates. I understand which calls go where in the following:
struct Foo
{
void bar() & {std::cout << "l-value"; }
void bar() && {std::cout << "r-value"; }
};
Foo f;
f.bar(); // calls l-value version
Foo().bar(); // call r-value version
But what is the practical use of such a distinction?
Upvotes: 2
Views: 71
Reputation: 76386
An rvalue method may safely move from or otherwise invalidate the invocant, because it won't be accessible afterwards (it must not return or store the reference, of course). So in cases where a destructive implementation of some operation is more efficient, you can provide it for rvalues only where it is safe to use. Moving from also comes into consideration for cast operators.
Upvotes: 2