g++ and clang++ different behaviour with recursive initialization of a static member

It is unspecified. Both compilers are right.

Here are the relevant pieces from cppreference “initialization”.

Static initialization

For all other non-local static and thread-local variables, Zero initialization takes place

So for all these variables, they are zero when the program loads. Then:

Dynamic initialization

After all static initialization is completed, dynamic initialization of non-local variables occurs in the following
situations:

1) Unordered dynamic initialization, which applies only to
(static/thread-local) class template static data members and … that aren’t explicitly specialized.

And these variables match the criteria. And then it says:

Initialization of such static variables is indeterminately sequenced
with respect to all other dynamic initialization ….

Which means that any sequence of initialization is fine. Both compilers are correct.

To avoid the issue use constexpr to force a “constant initialization” instead.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)