c++language-lawyerprvalue

Cv-qualification of prvalues during reference binding


Consider this example:

const int& r2 = 5;

The rules governing reference-initialization are found in [dcl.init.ref]/5:

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

  • (5.1) [..]
  • (5.2) [..]
  • (5.3) Otherwise, if the initializer expression
    • (5.3.1) is an rvalue [..] and “cv1 T1” is reference-compatible with “cv2 T2”, or
    • (5.3.2) [..]

then the initializer expression [..] is called the converted initializer. If the converted initializer is a prvalue, its type T4 is adjusted to type “cv1 T4” ([conv.qual]) and the temporary materialization conversion ([conv.rval]) is applied. In any case, the reference is bound to the resulting glvalue [..].

Here, The converted initializer is a prvalue of type T4 (int); then it's adjusted to cv1 T4 (const int). But [expr.type]/2 states:

If a prvalue initially has the type “cv T”, where T is a cv-unqualified non-class, non-array type, the type of the expression is adjusted to T prior to any further analysis.

One said to me that this rule ([expr.type]/2) does not apply to specifically this case. And I need to know the reason behind that.

I claim that the reason for not applying [expr.type]/2 is because initially (before adjustment) the type of the prvalue is T4 (int) i.e, it's cv-unqualified type, so [expr.type]/2 cannot be applied since the initial type of the prvalue is not cv-qualified type. Even after the adjustment to cv1 T4, the rule [expr.type]/2 also doesn't apply because cv1 T4 is not the initial type of the prvalue. My question is: Are those correct reasons?

Indeed both questions this and this are relatively similar but specifically I'm asking a specific, different question regarding the rule [expr.type]/2.


Solution

  • IMO the mentioned case (const int& r2 = 5;) should fall into [dcl.init.ref]/(5.4.2). I think this is related to a mischange introduced by P0135R1 and fixed by CWG2481.

    One said to me that this rule ([expr.type]/2) does not apply to specifically this case. And I need to know the reason behind that.

    I think that is a non-standard comprehension of the cases addressed by CWG2481, which should be equivalent to the standard one. That is just saying "cv-qualification is not stripped".


    Perhaps the use of "rvalue" in [dcl.init.ref]/(5.3.1) is wrong unintended and the value category should be "xvalue". I'll contact CWG chair for clarification.

    I've opened an editorial issue #5821 to ask whether "its type T4 is adjusted to type “cv1 T4”" (5.3) and "considering the type of the prvalue to be “cv1 T1”" (5.4.2) have the same meaning.


    Edit: CWG2657 has been opened for this.