Происходит ли фиаско порядка статической инициализации с модулями C++20?C++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Гость
 Происходит ли фиаско порядка статической инициализации с модулями C++20?

Сообщение Гость »


I was told to use the singleton pattern instead of having global objects, which have the “static” storage duration, to avoid the static initialization order fiasco. But now that I have C++20 modules, should I?
Static initialization order fiasco
The static initialization order fiasco refers to the ambiguity in the order that objects with static storage duration in different translation units are initialized in. If an object in one translation unit relies on an object in another translation unit already being initialized, a crash can occur if the compiler decides to initialize them in the wrong order. For example, the order in which files are specified on the command line may alter this order. The Construct on First Use Idiom can be used to avoid the static initialization order fiasco and ensure that all objects are initialized in the correct order.

Within a single translation unit, the fiasco does not apply because the objects are initialized from top to bottom.
Storage duration “static” storage duration.
The storage for the object is allocated when the program begins and deallocated when the program ends. Only one instance of the object exists. All objects declared at namespace scope (including global namespace) have this storage duration, plus those declared with or . See “Non-local variables” and “Static local variables” for details on initialization of objects with this storage duration.
As far as I know…
The “static initialization order fiasco” means that which objects with the “static” storage duration, such as global objects, in non-module files get initialized earlier is ambiguous. It might lead to a crash where an uninitialized global object is used. Many adopted the singleton pattern that features “construct on first use (lazy construction)” to avoid this.
Then, with C++20 modules…
The dependencies between C++ module files (“translation units”) are seemingly clear with the statements in them. Does the static initialization order fiasco matters and do I still have to use the singleton pattern, instead of having -able objects declared at the top level in C++ modules?


Источник: https://stackoverflow.com/questions/781 ... 20-modules
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Происходит ли фиаско порядка статической инициализации с модулями C++20?
    Anonymous » » в форуме C++
    0 Ответы
    26 Просмотры
    Последнее сообщение Anonymous
  • Происходит ли фиаско порядка статической инициализации с модулями C++20?
    Гость » » в форуме C++
    0 Ответы
    13 Просмотры
    Последнее сообщение Гость
  • Происходит ли фиаско по порядку статической инициализации с модулями C ++ 20?
    Anonymous » » в форуме C++
    0 Ответы
    3 Просмотры
    Последнее сообщение Anonymous
  • Использование C ++ Новое для обходного пути. Фиаско заказа инициализации статической инициализации
    Anonymous » » в форуме C++
    0 Ответы
    16 Просмотры
    Последнее сообщение Anonymous
  • Предотвратить порядок статической инициализации «фиаско», C ++
    Anonymous » » в форуме C++
    0 Ответы
    13 Просмотры
    Последнее сообщение Anonymous

Вернуться в «C++»