Title
Lifetime of references
Status
cd4
Section
6.7.5 [basic.stc]
Submitter
Mike Miller

Created on 2014-09-29.00:00:00 last changed 57 months ago

Messages

Date: 2015-09-15.00:00:00

Proposed resolution (September, 2015):

  1. Change 6.7.5 [basic.stc] paragraph 3 as follows:

  2. The storage duration categories apply to references as well. The lifetime of a reference is its storage duration.
  3. Change 6.7.3 [basic.life] paragraph 1 as follows:

  4. The lifetime of an object or reference is a runtime property of the object or reference. An object is said to have...
  5. Add the following as a new paragraph following 6.7.5 [basic.stc] paragraph 2:

  6. [Note: The lifetime of an array object starts as soon as storage with proper size and alignment is obtained, and its lifetime ends when the storage which the array occupies is reused or released. 11.9.3 [class.base.init] describes the lifetime of base and member subobjects. —end note]

    The lifetime of a reference begins when its initialization is complete. The lifetime of a reference ends as if it were a scalar object.

  7. Change 6.7.3 [basic.life] paragraph 3 as follows:

  8. The properties ascribed to objects and references throughout this International Standard apply for a given object or reference only during its lifetime. [Note:...

    Change Clause 7 [expr] paragraph 5 as follows:

    If an expression initially has the type “reference to T” (9.3.4.3 [dcl.ref], 9.4.4 [dcl.init.ref]), the type is adjusted to T prior to any further analysis. The expression designates the object or function denoted by the reference, and the expression is an lvalue or an xvalue, depending on the expression. [Note: Before the lifetime of the reference has started or after it has ended, the behavior is undefined (see 6.7.3 [basic.life]). —end note]

Drafting note: there is no change to 6.7.3 [basic.life] paragraph 4:

A program may end the lifetime of any object by reusing the storage which the object occupies or by explicitly calling the destructor for an object of a class type with a non-trivial destructor. For an object of a class type...
Date: 2016-02-15.00:00:00

[Adopted at the February, 2016 meeting.]

According to 6.7.5 [basic.stc] paragraph 3,

The storage duration categories apply to references as well. The lifetime of a reference is its storage duration.

This is clearly not correct; references can have static storage duration but be dynamically initialized. Consider an example like:

  extern int& r1;
  int& f();
  int& r2 = r1;  // #1
  int& r1 = f();
  int i = r2;    // #2

r1 is not initialized until after its use at #1, so the initialization of r2 should produce undefined behavior, as should the use of r2 at #2.

The description of the lifetime of a reference should be deleted from 6.7.5 [basic.stc] and it should be described properly in 6.7.3 [basic.life].

History
Date User Action Args
2017-02-06 00:00:00adminsetstatus: tentatively ready -> cd4
2015-11-10 00:00:00adminsetmessages: + msg5590
2015-11-10 00:00:00adminsetstatus: drafting -> tentatively ready
2015-05-25 00:00:00adminsetstatus: open -> drafting
2014-09-29 00:00:00admincreate