Goran Mitrovic
Goran Mitrovic

Reputation: 95

C++ overload resolution, conversion operators and const

In this case

void f(int *);
void f(const int *);
...
int i;
f(&i);

the situation is pretty clear - f(int *) gets called which seems right.

However, if I have this (it was done like that by mistake(*) ):

class aa
{
public:
    operator bool() const;
    operator char *();
};

void func(bool);

aa a;
func(a);

operator char *() gets called. I cannot figure out why would such decision path be better than going to operator bool(). Any ideas?

(*) If const is added to the second operator, the code works as expected, of course.

Upvotes: 4

Views: 624

Answers (3)

Columbo
Columbo

Reputation: 61009

Because for user-defined conversions with a conversion operator the conversion of the returned type to the destination type (i.e. char* to bool) is considered after the object argument conversion, i.e. the conversion of the object argument a to the implicit object parameter. [over.match.best]/1:

Given these definitions, a viable function F1 is defined to be a better function than another viable function F2 if for all arguments i, ICSi (F1) is not a worse conversion sequence than ICSi(F2), and then

  • for some argument j, ICSj(F1) is a better conversion sequence than ICSj(F2), or, if not that,

  • the context is an initialization by user-defined conversion (see 8.5, 13.3.1.5, and 13.3.1.6) and the standard conversion sequence from the return type of F1 to the destination type (i.e., the type of the entity being initialized) is a better conversion sequence than the standard conversion sequence from the return type of F2 to the destination type.

So because the implicit object parameter, which is a reference, is not a const-reference for operator char*, it is a better match according to the first bullet point.

Upvotes: 5

Ixrec
Ixrec

Reputation: 976

Your "aa" object is not const, so C++ prefers the non-const conversion.

If you make "aa" const, then the const bool() conversion operator will be used.

Upvotes: 0

Mark B
Mark B

Reputation: 96311

a is a non-const instance of aa so the non-const conversion operator is a better (exact) match than the const one (requires adding constness), even though the return types don't match as well.

Upvotes: 1

Related Questions