Applying the && modifier to class member functions

In addition to being applied to objects, you can also add the && modifier to a member function of a class, just as you can apply a const modifier to a member function. As in the case with the const modifier, a member function which has the && modifier is only permitted to be executed on an r-value.

struct Foo {   auto func() && {} }; auto a = Foo{}; a.func(); // Does not compile, 'a' is not an r-value std::move(a).func(); // Compiles Foo{}.func(); // Compiles

It might seem odd that one would ever want this behavior, but there are cases. We will investigate one of those cases in Chapter 9, Proxy Objects and Lazy Evaluation.

Get C++ High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.