2013-03-04 149 views
0

我有一个实现了单模板类:静态变量初始化

template<typename T> 
class Singleton { 
    static Singleton const& get() const { return _instance; } 
    private: 
     Singleton(); 
     static Singleton _instance; 
}; 

有此模板的一些特例,例如,用于Type1Type2。它的构造函数有互相呼叫get()。我看到一个问题。让我们假设的Type1Singleton在第一构造:

Singleton<Type1>::Singleton() { 
    Singletion<Type2>::get(); 
} 

所以Type2Singleton尚未建立。 get()会返回什么?

回答

1

您需要在get函数内使_instancestatic

template<typename T> 
class Singleton { 
    static Singleton const& get() const { 
     static Singleton _instance; 
     return _instance; 
    } 
    private: 
     Singleton(); 
}; 

这样,实例将在第一次调用get时被初始化。

+0

是的。好主意。但接下来会发生什么? 'Singleton :: Singleton()'调用正在构建的'Singleton '的'Singleton()'Singleton()''的执行需要'static'' _instance'变量。 – user14416 2013-03-04 02:28:31

+0

@ user14416可能未定义的行为。这是一个循环依赖,肯定是一个错误。 – Pubby 2013-03-04 02:37:21

+0

你的想法不适合我。在程序启动期间,我需要这些单例初始化。 – user14416 2013-03-04 02:38:01

0

的循环依赖是不好:

struct Singleton2; 

struct Singleton1 { 
    static Singleton1 const& get() { 
     static Singleton1 _instance; 
     return _instance; 
    } 
    private: 
     Singleton1(); 
     Singleton2 const& _s2; 
}; 

struct Singleton2 { 
    static Singleton2 const& get() { 
     static Singleton2 _instance; 
     return _instance; 
    } 
    private: 
     Singleton2(); 
     Singleton1 const& _s1; 
}; 

Singleton1::Singleton1() : _s2(Singleton2::get()) { } 
Singleton2::Singleton2() : _s1(Singleton1::get()) { } 

int main() 
{ 
    auto& s1 = Singleton1::get(); 
    auto& s2 = Singleton2::get(); 
} 

会导致失败(见http://liveworkspace.org/code/4rPFDo$0)。

就像在你需要打破周期所有的情况下,使链至少一个链接。

,不要求在施工期间给其他单身的参考解决它的明显的方式:

struct Singleton2; 

struct Singleton1 { 
    static Singleton1 const& get() { 
     static Singleton1 _instance; 
     return _instance; 
    } 
    private: 
     Singleton1() {} 
     static Singleton2 const& getOther(); 
}; 

struct Singleton2 { 
    static Singleton2 const& get() { 
     static Singleton2 _instance; 
     return _instance; 
    } 
    private: 
     Singleton2() {} 
     static Singleton1 const& getOther(); 
}; 

Singleton2 const& Singleton1::getOther() { return Singleton2::get(); } 
Singleton1 const& Singleton2::getOther() { return Singleton1::get(); } 

int main() 
{ 
    auto& s1 = Singleton1::get(); 
    auto& s2 = Singleton2::get(); 
} 

或者延迟初始化使用boost ::可选的boost ::轻量级或自定义“lazy_ptr”: https://stackoverflow.com/a/878298/85371