且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是全局变量的静态初始化在`main()`之前完成?

更新时间:2023-11-15 11:00:04

标准说,在转换单元中调用任何函数之前,所有对象都在同一翻译单元(也就是对应于单个源文件的目标文件)中初始化。在你的例子中,它看起来像是在同一个文件,所以 a 将被初始化 main()

The standard says that all objects are initialized in the same translation unit (aka object file which corresponds to a single source file) before any function is called in that translation unit. In your example, it looks like they are in the same file, so a will be initialized before main() is called.

标准允许在运行时加载DLL的情况下进行延迟初始化。如果允许你的代码运行时链接,你不能说一切都在 main()之前初始化。

The standard is allowing lazy initialization to occur in the event that a DLL is loaded at run time. If you allow run-time linkage of your code, you can't say that everything is initialized before main().