Consider the following scenario:
int f( int ); int f( int && ); int f( int const & ); int q = f( 3 );
Calling f(3) results in an ambiguous overload resolution. Eliminating f( int ) causes Clang and GCC to prioritize the rvalue reference over the lvalue reference. However, removing either reference overload leads to ambiguity with f( int ).
Overload resolution typically follows a strict partial ordering. However, in this case, int appears to be equivalent to two concepts that are not equivalent to each other. The specific rules governing this situation are as follows:
13.3.3.2 of the C standard provides a special rule for direct reference binding:
If S1 and S2 are reference bindings (8.5.3) and neither refers to an implicit object parameter of a non-static member function declared without a ref-qualifier, and S1 binds an rvalue reference to an rvalue and S2 binds an lvalue reference, then S1 is a better match than S2.
This rule does not apply when one of the initializations is not a reference binding.
The idea of making reference bindings a better match than non-reference bindings has been proposed. To initiate discussions on this topic, it is recommended to post such proposals to the ISO C Future Proposals forum.
The above is the detailed content of Why is Overload Resolution Ambiguous Between `int`, `int&&`, and `const int&`?. For more information, please follow other related articles on the PHP Chinese website!