Title
Restriction on reference to temporary as a constant expression
Status
cd4
Section
7.7 [expr.const]
Submitter
Richard Smith

Created on 2013-05-30.00:00:00 last changed 95 months ago

Messages

Date: 2014-11-15.00:00:00

[Moved to DR at the November, 2014 meeting.]

Date: 2014-02-15.00:00:00

Proposed resolution (February, 2014):

Change 7.7 [expr.const] paragraph 4 as follows:

A constant expression is either a glvalue core constant expression whose value refers to an object with static storage duration or to a function entity that is a permitted result of a constant expression (as defined below), or a prvalue core constant expression whose value is an object where, for that object and its subobjects:

  • each non-static data member of reference type refers to an object with static storage duration or to a function entity that is a permitted result of a constant expression, and

  • if the object or subobject is of pointer type, it contains the address of an object with static storage duration, the address past the end of such an object (5.7), the address of a function, or a null pointer value.

An entity is a permitted result of a constant expression if it is an object with static storage duration that is either not a temporary object or is a temporary object whose value satisfies the above constraints, or it is a function.

Date: 2013-09-15.00:00:00

Proposed resolution (September, 2013) [SUPERSEDED]:

Change 7.7 [expr.const] paragraph 4 as follows:

A constant expression is either a glvalue core constant expression whose value refers to an object with static storage duration or to a function entity that is a permitted result of a constant expression, or a prvalue core constant expression whose value is an object where, for that object and its subobjects:

  • each non-static data member of reference type refers to an object with static storage duration or to a function entity that is a permitted result of a constant expression, and

  • if the object or subobject is of pointer type, it contains the address of an object with static storage duration, the address past the end of such an object (7.6.6 [expr.add]), the address of a function, or a null pointer value.

An entity is a permitted result of a constant expression if it is an object with static storage duration that is either not a temporary or is a temporary whose value satisfies the above constraints, or it is a function.

Date: 2013-10-14.00:00:00

We're missing a restriction on the value of a temporary which is bound to a static storage duration reference:

  void f(int n) {
    static constexpr int *&&r = &n;
  }

This is currently valid, because &n is a core constant expression, and it is a constant expression because the reference binds to a temporary (of type int*) that has static storage duration (because it's lifetime-extended by the reference binding).

The value of r is constant here (it's a constant reference to a temporary with a non-constant initializer), but I don't think we should accept this. Generally, I think a temporary which is lifetime-extended by a constexpr variable should also be treated as if it were declared to be a constexpr object.

History
Date User Action Args
2017-02-06 00:00:00adminsetstatus: drwp -> cd4
2015-05-25 00:00:00adminsetstatus: dr -> drwp
2015-04-13 00:00:00adminsetmessages: + msg5352
2014-11-24 00:00:00adminsetstatus: ready -> dr
2014-03-03 00:00:00adminsetmessages: + msg4815
2014-03-03 00:00:00adminsetstatus: review -> ready
2013-10-14 00:00:00adminsetmessages: + msg4610
2013-10-14 00:00:00adminsetstatus: open -> review
2013-05-30 00:00:00admincreate