(notice: I'm referring to the current C++ standard)
I'm not really sure about this, but, if my interpretation of the standard is correct, the code should be fine and not UB.
The first initialization of that variable is the zero-initialization of objects with static storage duration that happens before any other
initialization takes place (§3.6.2 ¶1).
So, first of all i
is set to zero.
Then, dynamic initialization (i.e. non-zero and non-constant initialization) takes place, so it uses the current value of i
(0) to actually initialize it again. At the end it should evaluate to 1.
This seems confirmed by §8.5 ¶6, that explicitly says:
The memory occupied by any object of static storage duration shall be zero-initialized at program startup before any other initialization takes place. [Note: in some cases, additional initialization is done later. ]
(If you find some flaw in the analysis please just tell me in the comments and I'll be glad to correct/delete the answer, it's slippery floor and I'm conscious of it :) )