简体   繁体   English

关于非本地静态变量初始化的规则是什么?

[英]What are the rules regarding initialization of non-local statics?

Suppose I have a class whose only purpose is the side-effects caused during construction of its objects (eg, registering a class with a factory): 假设我有一个类,其唯一目的是在构造其对象期间引起的副作用(例如,在工厂注册一个类):

class SideEffectCauser {
public:
  SideEffectCauser() { /* code causing side-effects */ }
};

Also suppose I'd like to have an object create such side-effects once for each of several translation units. 还要假设我想让一个对象为几个翻译单元中的每一个创建一次这样的副作用。 For each such translation unit, I'd like to be able to just put an a SideEffectCauser object at namespace scope in the .cpp file, eg, 对于每个这样的翻译单元,我希望能够将SideEffectCauser对象放在.cpp文件中的命名空间范围内,例如,

SideEffectCauser dummyGlobal;

but 3.6.2/3 of the C++03 standard suggests that this object need not be constructed at all unless an object or function in the .cpp file is used, and articles such as this and online discussions such as this suggest that such objects are sometimes not initialized. 但C ++标准03 3.6.2 / 3认为,这需要的对象根本不构成除非使用在.cpp文件中的对象或功能,如文章和诸如在线讨论这一建议,这样对象有时未初始化。

On the other hand, Is there a way to instantiate objects from a string holding their class name? 另一方面, 有没有办法从持有类名的字符串中实例化对象? has a solution that is claimed to work, and I note that it's based on using an object of a type like SideEffectCauser as a static data member, not as a global, eg, 有一个声称可以工作的解决方案,我注意到它基于将SideEffectCauser之类的对象用作静态数据成员,而不是全局对象,例如,

class Holder {
  static SideEffectHolder dummyInClass;
};

SideEffectHolder Holder::dummyInClass;

Both dummyGlobal and dummyInClass are non-local statics, but a closer look at 3.6.2/3 of the C++03 standard shows that that passage applies only to objects at namespace scope. dummyGlobaldummyInClass都是非本地静态变量,但仔细查看C ++ 03标准的3.6.2 / 3可以发现,该段落仅适用于命名空间范围内的对象。 I can't actually find anything in the C++03 standard that says when non-local statics at class scope are dynamically initialized, though 9.4.2/7 suggests that the same rules apply to them as to non-local statics at namespace scope. 我实际上找不到C ++ 03标准中的任何内容说明动态初始化类范围内的非本地静态变量,尽管9.4.2 / 7建议将相同的规则应用于命名空间中的非本地静态变量范围。

Question 1: In C++03, is there any reason to believe that dummyInClass is any more likely to be initialized than dummyGlobal ? 问题1:在C ++ 03中,是否有任何理由相信dummyInClassdummyGlobal更有可能被初始化? Or may both go uninitialized if no functions or objects in the same translation unit are used? 或者,如果在同一翻译单元中未使用任何函数或对象,可能都未初始化?

Question 2: Does anything change in C++11? 问题2:C ++ 11中有什么变化吗? The wording in 3.6.2 and 9.4.2 is not the same as the C++03 versions, but, from what I can tell, there is no behavioral difference specified for the scenarios I describe above. 3.6.2和9.4.2中的措词与C ++ 03版本不同,但是据我所知,对于我上面描述的场景没有指定行为上的差异。

Question 3: Is there a reliable way to use objects of a class like SideEffectHolder outside a function body to force side-effects to take place? 问题3:是否存在可靠的方法来在函数体内使用诸如SideEffectHolder类的对象来强迫发生副作用?

I think the only reliable solution is to design this for specific compiler(s) and runtime. 我认为唯一可靠的解决方案是针对特定的编译器和运行时进行设计。 No standard covers the initialization of globals in a shared library which I think is the most intricate case, as this is much dependent on the loader and thus OS dependent. 我认为这是最复杂的情​​况,没有一个标准涵盖共享库中全局变量的初始化,因为这在很大程度上取决于加载程序,因此取决于操作系统。

Q1: No Q2: Not in any practical sense Q3: Not in a standard way Q1:否Q2:从任何实际意义上说,Q3:不是以标准方式

I'm using something similar with g++ / C++11 under Linux and get my factories registered as expected. 我在Linux下使用与g ++ / C ++ 11类似的东西,并按预期方式注册了我的工厂。 I'm not sure why you wouldn't get the functions called. 我不确定为什么您不会调用这些函数。 If what you describes is to be implemented it will mean that every single function in that unit has to call the initialization function. 如果要实现您所描述的内容,则意味着该单元中的每个单个函数都必须调用初始化函数。 I'm not too sure how that could be done. 我不太确定该怎么做。 My factories are also inside namespaces, although it is named namespaces. 我的工厂也位于名称空间中,尽管它被命名为名称空间。 But I don't see why it wouldn't be called. 但是我不明白为什么不叫它。

namespace snap {
namespace plugin_name {
class plugin_name_factory {
public:
  plugin_name_factory() { plugin_register(this, name); }
...
} g_plugin_name_factory;
}
}

Note that the static keyword should not be used anymore in C++ anyway. 请注意,无论如何在C ++中都不应再使用static关键字。 It is often slower to have a static definition than a global. 具有静态定义通常比全局定义慢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 非局部变量的动态初始化是线程安全的吗 - Is the dynamic initialization of the non-local variable thread-safe 静态和动态初始化仅适用于非局部变量吗? - Do static and dynamic initialization only apply to non-local variables? 如何理解允许实现在某些情况下将非局部变量的动态初始化视为静态初始化? - How to comprehend that an implementation is permitted to treat dynamic initialization of non-local variable as static initialization in some cases? 非局部非内联变量的初始化:它是否严格在调用main()函数之前进行? - The initialization of non-local non-inline variables: does it take place strictly before the `main()` function call? 在不同的翻译单元中对带有静态存储持续时间的相关非局部常量浮点变量进行常量初始化 - Constant initialization of dependent non-local constant float variables w/ static storage duration in different translation units 非本地函数调用本地类型 - non-local function call local type Friend函数类内定义仅允许在非本地类定义中使用。 这是什么意思? - Friend function in-class definition only allowed in non-local class definitions. What does it mean? 关于类模板中静态的编译器错误是什么意思? - What does this compiler error regarding statics in a class template mean? 在本地定义的结构中模拟非本地(或自由)变量 - Simulating non-local (or free) variables in a locally defined struct 如果全局 function 使用非局部变量是否正确,那么它是一个闭包? - Is it correct that if a global function uses non-local variables, then it's a closure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM