Movatterモバイル変換


[0]ホーム

URL:


This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 119a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2025-12-20


2102. Constructor checking innew-expression

Section:7.6.2.8  [expr.new]    Status:CD7    Submitter:Richard Smith    Date:2015-03-16

[Accepted as a DR at the November, 2023 meeting.]

According to 7.6.2.8 [expr.new] paragraph 25,

If thenew-expression creates an object or an arrayof objects of class type, access and ambiguity control aredone for the allocation function, the deallocation function(11.4.11 [class.free]), and the constructor(11.4.5 [class.ctor]).

The mention of “the constructor” here isstrange. For the “object of class type” case,access and ambiguity control are done when we performinitialization in paragraph 17, and we might not be calling aconstructor anyway (for aggregate initialization). Thisseems wrong.

For the “array of objects of class type”case, it makes slightly more sense (we need to check thetrailing array elements can be default-initialized) butagain (a) we aren't necessarily using a constructor, (b) weshould saywhich constructor — and we may needoverload resolution to find it, and (c) shouldn't this bepart of initialization, so we can distinguish between thecases where we should copy-initialize from{} andthe cases where we should default-initialize?

Additional notes (May, 2023):

It is unclear whether default-initialization is required to bewell-formed even for an array with no elements.

Proposed resolution (approved by CWG 2023-06-16):

  1. Insert a new paragraph before 7.6.2.8 [expr.new] paragraph 9:

    If the allocated type is an array,thenew-initializer is abraced-init-list, andtheexpression is potentially-evaluated and not a core constantexpression, the semantic constraints of copy-initializing ahypothetical element of the array from an empty initializer list arechecked (9.5.5 [dcl.init.list]). [ Note: The array can containmore elements than there are elements in thebraced-init-list,requiring initialization of the remainder of the array elements froman empty initializer list. -- end note ]

    Objects created by a new-expression have dynamic storage duration(6.8.6.5 [basic.stc.dynamic]). ...

  2. Change in 7.6.2.8 [expr.new] paragraph 25 as follows:

    If thenew-expression creates an object or an array of objectsof class type, access and ambiguity control are done for theallocation function, the deallocation function(6.8.6.5.3 [basic.stc.dynamic.deallocation]), and the constructor(11.4.5 [class.ctor]) selected for the initialization (ifany). If thenew-expression creates an array of objects ofclass type, the destructor is potentially invoked(11.4.7 [class.dtor]).
  3. Change in 7.6.2.8 [expr.new] paragraph 28 as follows:

    A declaration of a placement deallocation function matches thedeclaration of a placement allocation function if it has the samenumber of parameters and, after parameter transformations(9.3.4.6 [dcl.fct]), all parameter types except the firstare identical. If the lookup finds a single matching deallocationfunction, that function will be called; otherwise, no deallocationfunction will be called. If the lookup finds a usual deallocationfunction and that function, considered as a placement deallocationfunction, would have been selected as a match for the allocationfunction, the program is ill-formed. For a non-placement allocationfunction, the normal deallocation function lookup is used to find thematching deallocation function (7.6.2.9 [expr.delete]).In any case, the matching deallocation function (if any) shall benon-deleted and accessible from the point wherethenew-expression appears.
  4. Change in 9.5.1 [dcl.init.general] paragraph 7 as follows:

    Todefault-initialize an object of type T means:
    • ...
    • If T is an array type,the semantic constraints ofdefault-initializing a hypothetical element shall be met andeach element is default-initialized.
    • ...
  5. Change in 9.5.1 [dcl.init.general] paragraph 9 as follows:

    Tovalue-initialize an object of type T means:
    • ifIf T is a(possibly cv-qualified) class type (Clause 11 [class]),then
      • if T has either no default constructor(11.4.5.2 [class.default.ctor]) or a default constructor that isuser-provided or deleted, then the object is default-initialized;
      • otherwise, the object is zero-initialized and the semantic constraintsfor default-initialization are checked, and if T has a non-trivialdefault constructor, the object isdefault-initialized;.
    • ifIf T is an array type,the semanticconstraints of value-initializing a hypothetical element shall be metand each element is value-initialized;.
    • otherwiseOtherwise, the object iszero-initialized.



[8]ページ先頭

©2009-2026 Movatter.jp