Dynamic Initialization动态初始化的使用

本文探讨了C++中动态初始化的概念,并详细介绍了如何利用动态初始化收集类元信息,特别是在main函数或DLL全局函数执行前完成元信息收集的便利性。通过不完整类型定义的元信息收集器和宏包装,可以简化并隐藏元信息的注册过程。文中提到了VC系列编译器的实现,并展示了具体代码实现示例。
摘要由CSDN通过智能技术生成

之前看公司有人收集类元信息用的方法很巧妙,所以早就想写一篇关于动态初始化的博客,一则是想分享,二则是作为个人摘记.


在c++标准的initialization of non-local variable章节有如下描述

There are two broad classes of named non-local variables: those with static storage duration (3.7.1) and
those with thread storage duration (3.7.2). Non-local variables with static storage duration are initialized
as a consequence of program initiation. Non-local variables with thread storage duration are initialized as a
consequence of thread execution. Within each of these phases of initiation, initialization occurs as follows.

Together, zero-initialization and constant initialization are called static initialization ; all other initialization is 
dynamic initialization . Static initialization shall be performed before any dynamic initialization takes place. Dynamic initialization 
of a non-local variable with static storage duration is either ordered or unordered. Definitions of explicitly specialized class template
static data members have ordered initialization. 

It is implementation-defined whether the dynamic initialization of a non-local variable with static storage
duration is done before the first statement of main. If the initialization is deferred to some point in time
after the first statement of main, it shall occur before the first odr-use (3.2) of any function or variable
defined in the same translation unit as the variable to be initialized.


非局部静态变量的初始化是否发生在主函数main之前则以依赖于编译器的实现,在msdn上能看到如下文档

C Run-Time Error R6030
CRT not initialized
This error occurs if you are using the CRT, but the CRT startup code was not executed. 
It is possible to get this error if the linker switch /ENTRY is used to override the 
default starting address, usually mainCRTStartup, wmainC	RTStartup for a console 
EXE,WinMainCRTStartup or wWinMainCRTStartup for a Windows EXE, or _DllMainCRTStartupfor a DLL.
Unless one of the above functions is called on startup, the C Runtime will not be initialized.


比如我们使用vc100编译器,默认的入口函数wmainCRTStartup,当然项目配置或者链接器提供/ENTRY参数进行入口函数的指定,对于DLL又有所不同,执行的_DLLMainCRTStartup函数,一般来说这些函数做了如下事情

including calling _CRT_INIT, which initializes the C/C++ run-time library and invokes C++ constructors on static, non-local variables

所以对于VC系列编译器CRT提供了动态初始化的实现,在CRT Initialization文档部分,我们看到如下描述


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值