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

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

Сообщение Anonymous »


Мне посоветовали использовать шаблон Singleton вместо глобальных объектов, которые имеют «статическую» продолжительность хранения, чтобы избежать фиаско статического порядка инициализации. Но теперь, когда у меня есть модули C++20, стоит ли мне это делать?


Фиаско порядка статической инициализации
Фиаско статического порядка инициализации относится к неоднозначности порядка инициализации объектов со статическим сроком хранения в разных единицах трансляции. Если объект в одной единице трансляции полагается на уже инициализируемый объект в другой единице трансляции, сбой может произойти, если компилятор решит инициализировать их в неправильном порядке. Например, порядок, в котором 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++»