Maestro
Maestro

Reputation: 2562

C++ primer 5 ed. Overloaded Functions and User-Defined Conversion

Hello I am at chapter 14 in C++ primer 5 ed. I've understood anything from previous chapters. It is really a very good book. however when I've reached this, it is a bit ambiguous for me:

"In a call to an overloaded function, if two (or more) user-defined conversions provide a viable match, the conversions are considered equally good. The rank of any standard conversions that might or might not be required is not considered. Whether a built-in conversion is also needed is considered only if the overload set can be matched using the same conversion function. For example, our call to manip would be ambiguous even if one of the classes defined a constructor that required a standard conversion for the argument:

struct E {
     E(double);
     // other members
};
void manip2(const C&);
void manip2(const E&);
// error ambiguous: two different user defined conversions could be used
manip2(10); // manip2(C(10) or 
manip2(E(double(10)))

In this case, C has a conversion from int and E has a conversion from double. For the call manip2(10), both manip2 functions are viable:

manip2(const C&) is viable because C has a converting constructor that takes an int. That constructor is an exact match for the argument.

manip2(const E&) is viable because E has a converting constructor that takes a double and we can use a standard conversion to convert the int argument in order to use that converting constructor.

Because calls to the overloaded functions require different user-defined conversions from one another, this call is ambiguous. In particular, even though one of the calls requires a standard conversion and the other is an exact match, the compiler will still flag this call as an error.

Note In a call to an overloaded function, the rank of an additional standard conversion (if any) matters only if the viable functions require the same user-defined conversion. If different user-defined conversions are needed, then the call is ambiguous."

Upvotes: 0

Views: 93

Answers (0)

Related Questions