Maestro
Maestro

Reputation: 2552

Overload resolution and user-defined conversion

Consider this example:

struct Foo
{
    Foo(int){cout << "Foo(int)\n";}
    Foo(double){cout << "Foo(double)\n";}

    operator int()const{cout << "operator int()\n"; return 0;}
    operator double()const{cout << "operator double()\n"; return 0.;}
};

void bar(Foo){cout << "bar(Foo)\n";}
void bar(float){cout << "bar(float)\n";}



int main()
{

    int i = 5;
    bar(i); // whey bar(float) and not bar(Foo)?
}

Upvotes: 2

Views: 91

Answers (1)

Cortex0101
Cortex0101

Reputation: 927

Does it mean that standard conversion is preferred over user-defined conversion?

Yes. Standard conversions are always preferred over user-defined ones. See this

In deciding on the best match, the compiler works on a rating system for the way the types passed in the call and the competing parameter lists match up. In decreasing order of goodness of match:

  • An exact match, e.g. argument is a double and parameter is a double
  • A promotion
  • A standard type conversion
  • A constructor or user-defined type conversion

Upvotes: 4

Related Questions