Title
Object lifetime and aggregate initialization
Status
cd1
Section
6.7.3 [basic.life]
Submitter
Jack Rouse

Created on 1999-05-20.00:00:00 last changed 161 months ago

Messages

Date: 2002-04-15.00:00:00

[Moved to DR at 4/02 meeting.]

Date: 2001-04-15.00:00:00

Proposed resolution (04/01):

In 6.7.3 [basic.life] paragraph 1, change

If T is a class type with a non-trivial constructor (11.4.5 [class.ctor]), the constructor call has completed.

to

If T is a class type with a non-trivial constructor (11.4.5 [class.ctor]), the initialization is complete. [Note: the initialization can be performed by a constructor call or, in the case of an aggregate with an implicitly-declared non-trivial default constructor, an aggregate initialization (9.4.2 [dcl.init.aggr]).]
Date: 2004-09-10.00:00:00

Jack Rouse: 6.7.3 [basic.life] paragraph 1 includes:

The lifetime of an object is a runtime property of the object. The lifetime of an object of type T begins when:
  • storage with the proper alignment and size for type T is obtained, and
  • if T is a class type with a non-trivial constructor (11.4.5 [class.ctor] ), the constructor call has completed.
Consider the code:
    struct B {
        B( int = 0 );
        ~B();
    };

    struct S {
        B b1;
    };

    int main()
    {
        S s = { 1 };
        return 0;
    }
In the code above, class S does have a non-trivial constructor, the default constructor generated by the compiler. According the text above, the lifetime of the auto s would never begin because a constructor for S is never called. I think the second case in the text needs to include aggregate initialization.

Mike Miller: I see a couple of ways of fixing the problem. One way would be to change "the constructor call has completed" to "the object's initialization is complete."

Another would be to add following "a class type with a non-trivial constructor" the phrase "that is not initialized with the brace notation (9.4.2 [dcl.init.aggr] )."

The first formulation treats aggregate initialization like a constructor call; even POD-type members of an aggregate could not be accessed before the aggregate initialization completed. The second is less restrictive; the POD-type members of the aggregate would be usable before the initialization, and the members with non-trivial constructors (the only way an aggregate can acquire a non-trivial constructor) would be protected by recursive application of the lifetime rule.

History
Date User Action Args
2008-10-05 00:00:00adminsetstatus: wp -> cd1
2003-04-25 00:00:00adminsetstatus: dr -> wp
2002-05-10 00:00:00adminsetstatus: ready -> dr
2001-11-09 00:00:00adminsetstatus: review -> ready
2001-05-20 00:00:00adminsetmessages: + msg483
2001-05-20 00:00:00adminsetstatus: drafting -> review
2000-11-18 00:00:00adminsetmessages: + msg417
2000-11-18 00:00:00adminsetstatus: open -> drafting
1999-05-20 00:00:00admincreate