Is static object guaranteed to be initialized
I am trying to learn about initialization of static objects. Static
initialization seems pretty straight forward, assuming you understand
constant expressions and constexpr. Dynamic initialization seems quite a
bit more tricky.
[basic.start.init]/4
It is implementation-defined whether the dynamic initialization of a
non-local variable with static storage duration is done before the
first statement of main. If the initialization is deferred to some
point in time after the first statement of main, it shall occur
before the first odr-use (3.2) of any function or variable
defined in the same translation unit as the variable to be
initialized.
footnote 34
A non-local variable with static storage duration having initialization
with side-effects must be initialized even if it is not odr-used
(3.2, 3.7.1).
[basic.start.init]/5
It is implementation-defined whether the dynamic initialization of a
non-local variable with static or thread storage duration is done before
the first statement of the initial function of the thread. If the
initialization is deferred to some point in time after the first
statement of the initial function of the thread, it shall occur before the
first odr-use (3.2) of any variable with thread storage duration
defined in the same translation unit as the variable to be
initialized.
I assume that "the initial function of the thread" refers to main, and not
just threads started with std::thread.
h1.h
#ifndef H1_H_
#define H1_H_
extern int count;
No comments:
Post a Comment