Title
Cv-qualification adjustment when binding reference to temporary
Status
drwp
Section
9.4.4 [dcl.init.ref]
Submitter
Brian Bi

Created on 2022-11-10.00:00:00 last changed 4 months ago

Messages

Date: 2024-03-20.02:00:44

Proposed resolution (approved by CWG 2024-03-20):

  1. Change in 9.4.4 [dcl.init.ref] bullet 5.3 as follows:

    Otherwise, if the initializer expression
    • is an rvalue (but not a bit-field) or function lvalue and “cv1 T1” is reference-compatible with “cv2 T2”, or
    • has a class type (i.e., T2 is a class type), where T1 is not reference-related to T2, and can be converted to an rvalue or function lvalue of type “cv3 T3”, where “cv1 T1” is reference-compatible with “cv3 T3” (see 12.2.2.7 [over.match.ref]),
    then the initializer expression in the first case and the converted expression in the second case is called the converted initializer. If the converted initializer is a prvalue, let its type be denoted by T4; the temporary materialization conversion (7.3.5 [conv.rval]) is applied, considering the type of the prvalue to be is adjusted to type “cv1 T4” (7.3.6 [conv.qual]) and the temporary materialization conversion (7.3.5 [conv.rval]) is applied. In any case, the reference binds to the resulting glvalue (or to an appropriate base class subobject).
  2. Append to the example in 9.4.4 [dcl.init.ref] bullet 5.3 as follows:

      B&& rrb = x;  // binds directly to the result of operator B
    
      constexpr int f() {
        const int &x = 42;
        const_cast<int &>(x) = 1;  // undefined behavior
        return x;
      }
      constexpr int z = f();   // error: not a constant expression
    
  3. Change the example in 9.4.4 [dcl.init.ref] bullet 5.4 as follows:

      const double& rcd2 = 2;   // rcd2 refers to temporary with type const double and value 2.0
    
Date: 2024-03-15.00:00:00

[Accepted as a DR at the March, 2024 meeting.]

Core issue 2481 was resolved by clarifying that the temporary object in p5.4.2 is cv-qualified if the reference being initialized is cv-qualified. However, this is not the right bullet point for the example given,

  constexpr const int &r = 42;

Such an initialization would actually use bullet 5.3.1 instead. (5.4.2 would be used if the initializer were, for example, 3.14.) We therefore need to make a similar clarification in bullet 5.3, and ideally using the same language.

History
Date User Action Args
2024-07-20 13:52:34adminsetstatus: dr -> drwp
2024-04-05 21:43:46adminsetstatus: ready -> dr
2024-03-20 14:10:31adminsetstatus: tentatively ready -> ready
2023-11-16 21:27:26adminsetstatus: open -> tentatively ready
2022-11-27 21:43:11adminsetmessages: + msg7070
2022-11-10 00:00:00admincreate