5

Why is the conversion of CL instance 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&() leads to user-defined conversion
2). cl.operator int&() leads to user-defined conversion and then qualification conversion (int& to const int&)

First way is better than second way, isn't it? I saw Standard, but found nothing.

Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160
Denis
  • 2,786
  • 1
  • 14
  • 29

1 Answers1

5

This is because both conversions 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&)
{
}
Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160