Inspired by this question.
We know that global variables with non-constexpr
initializers undergo two different "initializations":
- First, the "static initialization", which zero-initializes them.
- Second, the "dynamic initialization", which uses the user-provided initializer.
Which of those initializations starts the variable lifetime? [basic.life]
is being surprisingly unhelpful:
The lifetime of an object ... begins when: ... its initialization (if any) is complete
I see several options:
- The last initialization starts the lifetime.
- The first initialization starts the lifetime.
- Each successive initialization destroys the existing object and creates a new one in its place.
(1) Would make the most sense, but it would make the static initialization of an object that will later be dynamically initialized mostly useless.
(2) Would have interesting effects. E.g. the static init order fiasco is suddenly not UB (by itself) anymore.
(3) Would be very weird.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…