Ambiguous conversion to link

Why is converting an instance of CL to const int& ambiguous here?

 struct CL { operator const int&() { } operator int&() { } }; void fnc(const int&) { } int main() { CL cl; fnc(cl); } 

There are two ways:
1). cl.operator const int&() results in a custom conversion
2). cl.operator int&() leads to a custom conversion and then to a qualification conversion ( int& to const int& )

The first way is better than the second, right? I saw the Standard, but did not find anything.

+5
source share
1 answer

This is because both transformations are applicable (equally good) in this context. That is, both int& and const int& can be bound by const int& .

The conversion would not be ambiguous if you had:

 void fnc(int&) { } 
+5
source

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


All Articles