Overload resolution between object, rvalue reference, link constant

Given all three functions, this call is ambiguous.

int f( int ); int f( int && ); int f( int const & ); int q = f( 3 ); 

Removing f( int ) causes Clang and GCC to prefer the rvalue link over the lvalue link. But instead, removing the two overload reference values โ€‹โ€‹in an ambiguity with f( int ) .

Overload resolution is usually done in terms of strict partial ordering, but int seems equivalent to two things that are not equivalent to each other. What are the rules here? I seem to recall a bug report.

Is it likely that int && may be preferable to int in a future standard? The link must be bound to the initializer, while the type of the object is not so limited. Therefore, an overload between T and T && can actually mean "use an existing facility if I was given ownership, otherwise make a copy." (This looks like a clean pass by value, but saves the overhead of moving.) Since these compilers are currently working, this must be done by overloading T const & and T && and explicitly copying. But I'm not even sure that this is standard.

+54
c ++ overloading c ++ 11 rvalue-reference c ++ 14
Jul 31 '13 at 4:38
source share
1 answer

What are the rules here?

Since there is only one parameter, the rule is that one of the three viable initializations of the parameters of this parameter should be a better match than the other two. When two initializations are compared, either one is better than the other, or none of them is better (they are indistinguishable).

Without special rules for linking direct links, all three of these initializations would be indistinguishable (in all three comparisons).

Special rules for binding direct links make int&& better than const int& , but none of them is better or worse than int . Therefore, there is no better match:

 S1 S2 int int&& indistinguishable int const int& indistinguishable int&& const int& S1 better 

int&& better than const int& due to 13.3.3.2:

S1 and S2 are binding bindings (8.5.3), and none of them refers to an implicit parameter of a non-static member function object declared without a ref-determinant, and S1 binds an rvalue link to an rvalue, and S2 binds an lvalue value to a link.

But this rule does not apply when one of the initializations is not a link binding.

Is there any chance int && could be preferable to int in a future standard? The link must be bound to the initializer, while the type of the object is not so limited. Thus, an overload between T and T && can effectively mean "use an existing facility if I have been given ownership, otherwise make a copy."

You propose making binding snapping a better match than snapping without a link. Why not post your idea for future isocpp offers . SO is not the best for subjective discussion / opinion.

+43
Jul 31 '13 at 4:54
source share



All Articles