Question Details

No question body available.

Tags

c++ type-conversion language-lawyer

Answers (1)

February 9, 2026 Score: 6 Rep: 20,344 Quality: High Completeness: 100%

This initialization is subject to [dcl.init.ref]/(5.4.1):

A reference to type “cv1 T1” is initialized by an expression of type “cv2 T2” as follows:

  • [...]
  • Otherwise, T1 shall not be reference-related to T2.
    • If T1 or T2 is a class type, 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 E of the call to the conversion function, as described for the non-reference copy-initialization, is then used to direct-initialize the reference using the form (E). For this direct-initialization, user-defined conversions are not considered.

Since the copy-initialization of an int object from S() is well-formed by calling operator long&, the result of conversion function call (which has type long&) is used to direct-initialize the int&& variable. This direct-initialization is well-formed per [dcl.init.ref]/(5.4.2):

  • Otherwise, the initializer expression is implicitly converted to a prvalue of type “T1”. The temporary materialization conversion is applied, considering the type of the prvalue to be “cv1 T1”, and the reference is bound to the result.