Reputation: 51465
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
Upvotes: 2
Views: 1001
Reputation: 224079
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.
Upvotes: 4
Reputation: 22290
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
Upvotes: 2