Function overloads priority (links)

void func(const int &) { std::cout << "c lv ref\n"; }
void func(int &&) { std::cout << "rv ref\n"; }

func(1);

Since the const lvalue reference can accept all data types (const and non lval, const and non rvalue), I wonder what the guarantee is that the above code will print "rv ref". Is it standardized or compiler dependent?

+4
source share
1 answer

My previous explanation was wrong - like the TCs mentioned in the comments , both overloads have the same implicit sequence of transformations, and the ambiguity occurs as a special tie-breaker defined in the standard.


§13.3.3.1.4 [over.ics.ref], . 1

([dcl.init.ref]) , - , , , ([over.best.ics]). [...]

, , , . .

-:

§13.3.3.2 [over.ics.rank], . 3.2.3

[ S1 , S2, ...] S1 S2 ([dcl.init.ref ]), -, -, S1 rvalue rvalue, S2 lvalue. [...]

, :

  • , rvalue rvalue, .

  • , rvalue lvalue, , .


[...], S1 S2 [...]

, const r const&, :

§8.6.3 [dcl.init.ref], par 5.2

"cv1 T1" "cv2 T2" :

     

[ lvalue ...]

     

lvalue const (.. cv1 const), , rvalue.

     

[...] rvalue ( -) lvalue, "cv1 T1" - "cv2 T2"

+6

Source: https://habr.com/ru/post/1665987/


All Articles