[Bug lto/105876] use of -flto with g++ changes global extern const std::string initialization in unexpected manner

pinskia at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue Jun 7 20:11:02 GMT 2022


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105876

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Kevin Hendricks from comment #3)
> I thought the C++ spec said that static initialization is done in two
> phases.  global (extern cost) variable are always initialized in the order
> they are declared first and local (dynamic) static initialization is always
> done second (and order across compilation units is undefined as your say).

No you misunderstood the standard. It is done in two phases but it is not const
vs non-const rather static vs dynamic on the initializer part.
That is:
If you had:
const int t = 1;
int t1 = t;

The above in two different TUs, it would be still defined but if you had (in
different TUs):
const int t = f();
int t1 = t;

It would be unspecifized if t is initialized before t1. the standard specifies
that the order in one Translation Unit is defined though. So those are both
defined if they appear in that order inside one TU.


More information about the Gcc-bugs mailing list