Apparently, overloading on ref-qualifiers is not allowed – this code won't compile if you remove either &
or &&
(just the tokens, not their functions):
#include <iostream>
struct S {
void f() & { std::cout << "Lvalue" << std::endl; }
void f() && { std::cout << "Rvalue" << std::endl; }
};
int main()
{
S s;
s.f(); // prints "Lvalue"
S().f(); // prints "Rvalue"
}
In other words, if you have two functions of the same name and type, you have to define both if you define either. I assume this is deliberate, but what's the reason? Why not allow, say, calling the &&
version for rvalues if it's defined, and the "primary" f()
on everything else in the following variation (and vice-versa – although that would be confusing):
struct S {
void f() { std::cout << "Lvalue" << std::endl; }
void f() && { std::cout << "Rvalue" << std::endl; }
};
In other words, let them act similar to template specializations with respect to the primary template.