Question

How does C++ determine implicit conversion/construction of objects few levels deep? for example:

struct A {};
struct B: A {};
struct C { operator B() { return B(); } };

void f(A a) {}

int main(void)
{
    f(C());
}

Does it create tree of all possible conversions and chooses appropriate terminal? Something else? Thanks

Was it helpful?

Solution

The call to f() would need two conversions, one user-defined conversion (C to B) and one built-in conversion (derived-to-base: B to A). Calls with non-matching arguments succeed when they would need zero or one user-defined conversions. If different conversions (built-in or user-defined) would succeed, then, if all possible ways are equal in the number/kind of conversions they need, the call is ambiguous and the compiler needs to emit a diagnostic.

How compilers implement this isn't specified by the standard.

OTHER TIPS

The standard doesn't specify this. It only specifies the outcome. Each different compiler vendor can implement this any way they choose, as long as they give the correct result.

So there's probably a whole bunch of different approaches out there

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top