void func(const int &) { std::cout << "c lv ref\n"; }
void func(int &&) { std::cout << "rv ref\n"; }
func(1);
Since const lvalue reference is able to accept every kind of data (const and non- lval, const and non- rvalue), I wonder what is the guarantee that the code above will print "rv ref". Is it standardized or compiler dependent?
My previous explanation was incorrect - as T.C. mentioned in the comments, both overloads have the same implicit conversion sequence, and the disambiguation happens as a special tie-breaker defined in the standard.
If the argument types are the same, the implicit conversion sequence applied to both overloads is the identity conversion. This does not yet disambiguate the two functions.
The disambiguation is specified as a tie-breaker here:
The quotes imply that, in the case of two identity implicit conversion sequences:
The sequence binding an rvalue to rvalue reference is the best one.
The sequence binding an rvalue to an lvalue reference is worse than the aforementioned one.
The reason why
const
is required to bind rvalues toconst&
can be found here: