Given the following example,
struct S
{
operator const double&();
}
const int& ref = S();
First per [dcl.init.ref]/5:
A reference to type “
cv1 T1” is initialized by an expression of type “cv2 T2” as follows:
Taking "cv1 T1" as const int and "cv2 T2" as S.
Skipping all discarded bullets until we reach to [dcl.init.ref]/(5.4) which says:
(5.4) Otherwise:
(5.4.1) If
T1orT2is a class type andT1is not reference-related toT2, user-defined conversions are considered using the rules for copy-initialization of an object of type “cv1 T1” by user-defined conversion ([dcl.init], [over.match.copy], [over.match.conv]); the program is ill-formed if the corresponding non-reference copy-initialization would be ill-formed. The result of the call to the conversion function, as described for the non-reference copy-initialization, is then used to direct-initialize the reference. For this direct-initialization, user-defined conversions are not considered.(5.4.2) [..]
I think all we're agree on that this bullet (5.4.1) is one satisfied because T2 is a class type, and T1 is not reference-related to T2. But my problem at this point is that I can't understand the rest of the wording, and how it's related to each other.
Given the first sentence: "If T1 or T2 is a class type and T1 is not reference-related to T2, user-defined conversions are considered using the rules for copy-initialization of an object of type “cv1 T1” by user-defined conversion" What's the intention of the bold part? And the entity being initialized is "lvalue reference to cv1 T1" not an object of type "cv1 T1". So why the word "object" is mentioned here? Also, what're those rules of "copy-initialization"?
Given the second sentence: "The result of the call to the conversion function, as described for the non-reference copy-initialization, is then used to direct-initialize the reference.". Does this mean that we have to go back again to the beginning of [dcl.init.ref]/5? and why?
The last thing I need to understand is that the reference being initialized is of type const int&, and the called conversion function returns const double&; so how ref can bind to const double which is of an unrelated type?