c++ - Overload resolution between object, rvalue reference, const reference -
given 3 functions, call ambiguous.
int f( int ); int f( int && ); int f( int const & ); int q = f( 3 );
removing f( int )
causes both clang , gcc prefer rvalue reference on lvalue reference. instead removing either reference overload results in ambiguity f( int )
.
overload resolution done in terms of strict partial ordering, int
seems equivalent 2 things not equivalent each other. rules here? seem recall defect report this.
is there chance int &&
may preferred on int
in future standard? reference must bind initializer, whereas object type not constrained. overloading between t
, t &&
mean "use existing object if i've been given ownership, otherwise make copy." (this similar pure pass-by-value, saves overhead of moving.) these compilers work, must done overloading t const &
, t &&
, , explicitly copying. i'm not sure strictly standard.
what rules here?
as there 1 parameter, rule 1 of 3 viable parameter initializations of parameter must a better match both other two. when 2 initializations compared, either 1 better other, or neither better (they indistinguishable).
without special rules direct reference binding, 3 initializations mentioned indistinguishable (in 3 comparisons).
the special rules direct reference binding make int&&
better const int&
, neither better or worse int
. therefore there no best match:
s1 s2 int int&& indistinguishable int const int& indistinguishable int&& const int& s1 better
int&&
better const int&
because of 13.3.3.2:
s1 , s2 reference bindings (8.5.3) , neither refers implicit object parameter of non-static member function declared without ref-qualifier, , s1 binds rvalue reference rvalue , s2 binds lvalue reference.
but rule not apply when 1 of initializations not reference binding.
is there chance int && may preferred on int in future standard? reference must bind initializer, whereas object type not constrained. overloading between t , t && mean "use existing object if i've been given ownership, otherwise make copy."
you propose make reference binding better match non-reference binding. why not post idea isocpp future proposals. not best subjective discussion / opinion.
Comments
Post a Comment