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


2543.constinit and optimized dynamic initialization

Section:9.2.7  [dcl.constinit]    Status:C++23    Submitter:Zhihao Yuan    Date:2022-03-01    Liaison:(EWG)

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

Subclause 9.2.7 [dcl.constinit] paragraph 2 states:

If a variable declared with theconstinit specifier hasdynamic initialization (6.10.3.3 [basic.start.dynamic]), the program isill-formed. [Note: The constinit specifier ensures that thevariable is initialized during static initialization(6.10.3.2 [basic.start.static]). —end note]

Subclause 6.10.3.2 [basic.start.static] paragraph 3 gives permissionfor an implementation to perform static initialization in lieu ofdynamic initialization:

An implementation is permitted to perform the initialization of avariable with static or thread storage duration as a staticinitialization even if such initialization is not required to be donestatically, provided that ...

constinit will assuredly not give a diagnostic forvariables that are constant initialized (7.7 [expr.const] paragraph 2), because those are required to be statically initializedand thus will never be dynamically initialized. Conversely,constinit is guaranteed to give a diagnostic for variablesthat cannot be statically initialized, for example those with aninitializer whose value depends on runtime conditions.

Between those boundaries, it is unclear whetherconstinitought to give a diagnostic for variables whose initializer doesnot satisfy the constraints of constant-initialized, yet theimplementation takes advantage of the permission to turn dynamicinitialization into static initialization. For example,

  float f;  constinit int * pi = (int*) &f;    // reinterpret_cast, not constant-initialized

The current wording seems to imply thatconstinitaccurately reflects whether dynamic initialization was turned intostatic initialization by the implementation. However, that isimpossible to implement, because such decisions are often made by theoptimizer, which runs later than the compiler front-endinterpreting the program text containingconstinit.

There is value in permittingconstinit not to diagnosesome of the dynamic initializations that are turned into staticinitializations.

There is also value in having portable semantics ofconstinit.

See alsoissue 2536.

Notes from the November, 2022 meeting

CWG seeks the advice of EWG how to proceed with this issue,viacplusplus/papers#1379.

EWG 2023-01-19

EWG resolved to desire portable, guaranteed semanticsforconstinit. That means,constinit should producea diagnostic if de-jure dynamic initialization is turned into de-factostatic initialization as permitted by6.10.3.2 [basic.start.static] paragraph 3.

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

Change in 9.2.7 [dcl.constinit] paragraph 2 as follows:

If a variable declared with theconstinit specifier hasdynamic initialization (6.10.3.3 [basic.start.dynamic]), the program isill-formed, even if the implementation would perform thatinitialization as a static initialization(6.10.3.2 [basic.start.static]). [Note 1: The constinitspecifier ensures that the variable is initialized during staticinitialization(6.10.3.2 [basic.start.static]). —endnote]



[8]ページ先頭

©2009-2026 Movatter.jp