The &&
and &
qualifiers on a member function like that indicates the following:
- The
&
qualified function will be called on lvalue instances of the object
- The
&&
qualified function will be called on rvalue instances of the object
Example:
#include <iostream>
class Example
{
private:
int whatever = 0;
public:
Example() = default;
// Lvalue ref qualifier
void getWhatever() & { std::cout << "This is called on lvalue instances of Example\n"; }
// Rvalue ref qualifier
void getWhatever() && { std::cout << "This is called on rvalue instances of Example\n"; }
};
int main()
{
// Create example
Example a;
// Calls the lvalue version of the function since it's called on an lvalue
a.getWhatever();
// Calls rvalue version by making temporary
Example().getWhatever();
return 0;
}
The output of this is:
This is called on lvalue instances of Example
This is called on rvalue instances of Example
Since in the first function call we are calling it on an lvalue instance of Example
, but in the second call we are making it a temporary on which we call the function, invoking the rvalue qualified function.