Do objects of built-in types have special static initialisation order precedence?

前端 未结 4 540
梦如初夏
梦如初夏 2021-01-18 10:42

I\'d have expected the following code to yield a segmentation fault (or otherwise UB):

struct T {
   T();
};

T t;
char const* str = \"Test string\";

T::T()         


        
4条回答
  •  被撕碎了的回忆
    2021-01-18 11:20

    str is initialized by a constant expression and const char * is a POD type (C++03 terms, but C++11 it is analogous, but with different terms and way more allowed cases). Such an initialization is done in static initialization phase, and the static initialization phase has no issue of order. It happens before any dynamic initialization. t is initialized in the dynamic initialization phase.

提交回复
热议问题