【发布时间】:2013-10-29 12:45:34
【问题描述】:
在文件maybe_use_foo.cpp中:
namespace {
class Foo { /* ... */ };
Foo* const the_foo = new Foo;
}
void funtion_callable_from_another_tu_during_process_wide_initialization() {
// If we haven't yet run static initialization for this TU,
// but some other static initializer called us, ignore the request.
if (!the_foo)
return;
// OK, static initializers for this TU have run, foo exists, use it.
the_foo->doSomething();
}
那么,无论上述是否可取,它总是有效吗?在我看来,它假设在 TU 的静态初始化运行之前静态初始化为零。 C++ 标准(C++03?C++11?)能保证吗?
另一种提问方式是询问当解释为 Foo* 时,哪些值序列保存在“the_foo”的存储中。肯定是{NULL/nullptr, new Foo},还是{undefined, new Foo},还是别的什么?
请不要建议其他组织方式:我不是在寻找如何更好地做到这一点的建议,我是在寻找对技术合法性的更深入了解。
【问题讨论】:
标签: c++ static initialization standards