Phi*_*tor 2 c++ performance design-patterns
我有一个单例实现,但我不确定它包含哪些缺点。有人能告诉我这个实施有多好吗?
template <class Child>
class Singleton {
public:
inline static Child& Instance() {
return Instance_;
}
Singleton(const Singleton&) = delete;
Singleton(Singleton&&) = delete;
Singleton& operator=(const Singleton&) = delete;
Singleton& operator=(Singleton&&) = delete;
protected:
Singleton() = default;
private:
static Child Instance_;
};
template <typename Child> Child Singleton<Child>::Instance_;
Run Code Online (Sandbox Code Playgroud)
我知道 Scott Meyers 的 Singleton 实现定义了函数Instance_内部的静态GetInstance()。
inline static Child& Instance() {
static Child Instance_;
return Instance_;
}
Run Code Online (Sandbox Code Playgroud)
但这是否会带来额外的开销,因为每次调用该函数时都必须检查该函数是否Instance_已初始化。