Is static initialization guaranteed?

Take a look at this code:

struct Foo {
        void *ptr;

        constexpr Foo() : ptr(nullptr) { }
};

Foo f;

Is it guaranteed to fbe statically initialized?

Clang uses static initialization here, but MSVC does not .

+4
source share
3 answers

Yes, the Standard says it fwill initialize by default:

[basic.start.init] / 2:

o , , , constexpr o , [ : - ]. :

  • ... [ ]

  • , ,

  • ... [ , ]

, - ; - . .

- Foo, .

MSVC f.

+3

, . - .

, .

[edit] Clang - - .

0

I believe this will also be in msvc (at least against 2015) if you have to declare f itself as constexpr Foo f;

0
source

Source: https://habr.com/ru/post/1686265/


All Articles