This question is almost identical to Conversion operator in direct-initialization, however, this is about something I noticed in implementations.
Consider the following:
struct A { A(); A(A&&); };
struct B { operator A(); };
void f()
{
B b;
A a(b);
}
My reading of the standard says that because this direct-initialization, where the source and destination types differ, only constructors are considered. The constructor A(A&&) is selected, and the parameter is bound to the result the conversion function, yielding a total of two calls: operator A()
, A(A&&)
, as [dcl.init]/17.6.2.1 is the only sub clause that would apply here.
However, this is not the behavior displayed by gcc, msvc, and clang, or icc which all say that only operator A()
is called. Am I correct to assume that this is just compiler optimization?
I see no reason for the converting constructor not to be used here solely, other than the fact that I cannot find any wording that describes this behavior.