Lifetime of temporaries in default initialization of class arrays
6.7.7 [class.temporary]
Jack Rouse

Created on 1999-06-03.00:00:00 last changed 161 months ago


Date: 2001-04-15.00:00:00

[Moved to DR at 4/01 meeting.]

Date: 2000-04-15.00:00:00

Proposed resolution (04/01):

The proposed resolution is contained in the proposal for issue 201.

Date: 2004-09-10.00:00:00

Jack Rouse: 6.7.7 [class.temporary] states that temporary objects will normally be destroyed at the end of the full expression in which they are created. This can create some unique code generation requirements when initializing a class array with a default constructor that uses a default argument. Consider the code:

    struct T {
       int i;
       T( int );

    struct S {
       S( int = T(0).i );

    S* f( int n )
       return new S[n];
The full expression allocating the array in f(int) includes the default constructor for S. Therefore according to 6.9.1 [intro.execution] paragraph 14, it includes the default argument expression for S(int). So evaluation of the full expression should include evaluating the default argument "n" times and creating "n" temporaries of type T. But the destruction of the temporaries must be delayed until the end of the full expression so this requires allocating space at runtime for "n" distinct temporaries. It is unclear how these temporaries are supposed to be allocated and deallocated. They cannot readily be autos because a variable allocation is required.

I believe that many existing implementations will destroy the temporaries needed by the default constructor after each array element is initialized. But I can't find anything in the standard that allows the temporaries to be destroyed early in this case.

I think the standard should allow the early destruction of temporaries used in the default initialization of class array elements. I believe early destruction is the status quo, and I don't think the users of existing C++ compilers have been adversely impacted by it.

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:00adminsetmessages: + msg683
2001-05-20 00:00:00adminsetstatus: ready -> dr
2000-11-18 00:00:00adminsetstatus: review -> ready
2000-05-21 00:00:00adminsetmessages: + msg337
2000-05-21 00:00:00adminsetstatus: drafting -> review
2000-02-23 00:00:00adminsetstatus: open -> drafting
1999-06-03 00:00:00admincreate