Dynarray Allocation Context

ISO/IEC JTC1 SC22 WG21 N4043 - 2014-05-22

Lawrence Crowl, [email protected]

Contents

Introduction
Solution
Wording changes
    23.3.4.2 dynarray constructor and destructor [dynarray.cons]

Introduction

The dynarray class ( N3662 C++ Dynamic Arrays and N3820 Working Draft, Technical Specification — Array Extensions) and any other named class attempting to use the execution stack for allocation may suffer failure if the class is reallocated with in-place destruction and a placement new. This problem is described in N3899 Nested Allocation section Nested Lifetime. We need specific wording to address this case.

Solution

We choose to permit the stack optimization for constructors called an automatic variables and then only if not called with the placement new operator. This change effectively requires the compiler to recognize the context of a constructor call and, when appropriate, change the implementation to an alternate implementation.

Wording changes

The proposed wording changes are relative to N3820 Working Draft, Technical Specification — Array Extensions).

23.3.4.2 dynarray constructor and destructor [dynarray.cons]

Edit the effects paragraph for the explicit single-argument constructor as follows.

Effects: Allocates storage for c elements. May or may not invoke the global operator new. The c elements of the dynarray are default-initialized (8.5). When invoked for an object with other than automatic storage duration, or when invoked via a placement new, allocates elements with the global operator new. Otherwise may allocate elements with the global operator new or may use another mechanism.

Edit the effects paragraph for the two-argument constructor as follows.

Effects: Allocates storage for c elements. May or may not invoke the global operator new. The c elements of the dynarray are direct-initialized ([decl.init]) with argument v. When invoked for an object with other than automatic storage duration, or when invoked via a placement new, allocates elements with the global operator new. Otherwise may allocate elements with the global operator new or may use another mechanism.

Edit the effects paragraph for the copy constructor as follows.

Effects: Allocates storage for d.size() elements. The c elements of the dynarray are direct-initialized ([decl.init]) with the corresponding elements of d. May or may not invoke the global operator new. When invoked for an object with other than automatic storage duration, or when invoked via a placement new, allocates elements with the global operator new. Otherwise may allocate elements with the global operator new or may use another mechanism.

Edit the effects paragraph for the allocator constructors.

Effects: Equivalent to the preceding constructors except that each element is constructed with uses-allocator construction ([allocator.uses.construction]) where the global operator new would have been used.