Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[basic.life] Does zero-initialization as a static initialization begin the lifetime of an object? CWG2821 #4802

Closed
xmh0511 opened this issue Aug 12, 2021 · 4 comments
Labels
cwg Issue must be reviewed by CWG.

Comments

@xmh0511
Copy link
Contributor

xmh0511 commented Aug 12, 2021

int i = i;  // #1 static storage duration

[basic.life] p1 says

A variable is said to have vacuous initialization if it is default-initialized and, if it is of class type or a (possibly multi-dimensional) array thereof, that class type has a trivial default constructor. The lifetime of an object of type T begins when:

  • storage with the proper alignment and size for type T is obtained, and
  • its initialization (if any) is complete (including vacuous initialization) ([dcl.init]),

The declaration at #1 will undergo initialization twice. One happens in static initialization(zero-initialization), the other happens in dynamic initialization(whose initializer is itself).

However, the second bullet only requires that its initialization is complete, which initialization does it refer to in the intent here? Before the dynamic initialization occurs, i has been statically initialized to be 0, which can still say its initialization is complete. Does it mean the lifetime of i at this point has begun?

I don't know why do we change the original second bullet of the c++17 version to the current wording.

if the object has non-vacuous initialization, its initialization is complete,

I think the older version's intent is clearer to me, which means if the initialization for the object is not non-vacuous, only the specified initialization is complete will the lifetime of the object begin. Except that the possible defect that it didn't say whether the vacuous initialization contributes to this rule.

Could we just say

its dynamic initialization (if any) is complete (including vacuous initialization) ([dcl.init])

@jensmaurer
Copy link
Member

We can't access out-of-lifetime variables. However, the example at the bottom of [basic.start.static] p3 makes it clear that the intermediate state of "zero-initialized, but not yet dynamic-initialized" is a perfectly fine state.

In any case, this is not an editorial issue; maybe CWG wishes to somehow remove the double-initialization to make this clear, instead of saying something like "its first initialization" in [basic.life].

@jensmaurer jensmaurer added the cwg Issue must be reviewed by CWG. label Aug 12, 2021
@jensmaurer jensmaurer changed the title Does zero-initialization as a static initialization begin the lifetime of an object? [basic.life] Does zero-initialization as a static initialization begin the lifetime of an object? Aug 12, 2021
@geryogam
Copy link
Contributor

However, the second bullet only requires that its initialization is complete, which initialization does it refer to in the intent here?

@xmh0511 Excellent question.

maybe CWG wishes to somehow remove the double-initialization to make this clear, instead of saying something like "its first initialization" in [basic.life].

@jensmaurer Interesting.

@jensmaurer
Copy link
Member

CWG2821

@jensmaurer jensmaurer changed the title [basic.life] Does zero-initialization as a static initialization begin the lifetime of an object? [basic.life] Does zero-initialization as a static initialization begin the lifetime of an object? CWG2821 Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cwg Issue must be reviewed by CWG.
Projects
None yet
Development

No branches or pull requests

3 participants