C++0x对局部静态初始化作出了线程安全的要求,singleton的写法可以回归到最原始的方式

来源:互联网 发布:c语言short类型 编辑:程序博客网 时间:2024/04/28 02:54
class Foo{public:    staticFoo& instance(void)    {        static Foo s_instance;        return s_instance;    }};以下摘自ISOIEC 14882 C++116.7.4The zero-initialization (8.5) of all block-scope variables with static storage duration (3.7.1) or thread storage duration (3.7.2) is performed before any other initialization takes place. Constant initialization (3.6.2) of a block-scope entity with static storage duration, if applicable, is performed before its block is first entered. An implementation is permitted to perform early initialization of other block-scope variables with static or thread storage duration under the same conditions that an implementation is permitted to statically initialize a variable with static or thread storage duration in namespace scope (3.6.2). Otherwise such a variable is initialized the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization. If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration. If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.88 If control re-enters the declaration recursively while the variable is being initialized, the behavior is undefined.
原创粉丝点击