struct A { }; struct B{ operator A() {}; }; int main(){ B b; const A& a = b; // OK } The rules governing reference initialization can be found in [dcl.init.ref]/5:
A reference to type “
cv1 T1” is initialized by an expression of type “cv2 T2” as follows:
- (5.3) Otherwise, if the initializer expression
- (5.3.1) [..]
- (5.3.2) has a class type (i.e.,
T2is a class type), whereT1is not reference-related toT2, and can be converted to an rvalue or function lvalue of type “cv3 T3”, where “cv1 T1” is reference-compatible with “cv3 T3” (see [over.match.ref]) [..]
I think bullet (5.3.2) is applicable here: the initializer expression has class type B, and A is not reference-related to B. Am I correct?
And as far as I understood, cv3 T3 is the type that the conversion function B::operator A yields, which is A. Hence, the initializer expression b can be converted to an rvalue of type A via B::operator A, and cv1 T1 (const A) is reference-compatible with cv3 T3 (A). Assuming I correct at this point, I will continue.
Jumping to [over.match.ref]:
Under the conditions specified in
[dcl.init.ref], a reference can be bound directly to the result of applying a conversion function to an initializer expression. Overload resolution is used to select the conversion function to be invoked. Assuming that “reference tocv1 T” is the type of the reference being initialized, and “cv S” is the type of the initializer expression, withSa class type, the candidate functions are selected as follows:
- (1.1) — The conversion functions of
Sand its base classes are considered. Those non-explicit conversion functions that are not hidden withinSand yield type “lvalue reference tocv2 T2” (when initializing an lvalue reference or an rvalue reference to function) or “cv2 T2” or “rvalue reference to cv2 T2” (when initializing an rvalue reference or an lvalue reference to function), where “cv1 T” is reference-compatible with “cv2 T2”, are candidate functions [..]
I'm initializing an lvalue reference, so only conversion functions that yield an lvalue reference are considered. But the conversion function B::operator A yields an rvalue, not an lvalue. So what does this mean? Is B::operator A a candidate? Am I misreading any wording?