使用 CRTP 实现单例



阅读此答案后,我尝试实现一些简单的CRTP用法。我想我会尝试实现单例(是的,我知道 - 它仅用于实践和研究(模式,因为链接答案已经做到......除了它不编译的事实。

引用的代码如下:

template <class ActualClass> 
class Singleton
{
public:
static ActualClass& GetInstance()
{
if(p == nullptr)
p = new ActualClass;
return *p; 
}
protected:
static ActualClass* p;
private:
Singleton(){}
Singleton(Singleton const &);
Singleton& operator = (Singleton const &); 
};
template <class T>
T* Singleton<T>::p = nullptr;
class A: public Singleton<A>
{
//Rest of functionality for class A
};

然后我将其"现代化"为:

template <class T>
class Singleton {
public:
Singleton()                              = delete;
Singleton(const Singleton&)              = delete;
Singleton(Singleton&&)                   = delete;
Singleton& operator = (const Singleton&) = delete;
Singleton& operator = (Singleton&&)      = delete;
static T& get_instance() {
if(!instance)
instance = new T;
return *instance;
}
protected:
static inline T* instance = nullptr;
};
class A: public Singleton<A> {
//Rest of functionality for class A
};

然后,我尝试创建对该实例的引用:

auto& x = A::get_instance();

显然没有编译。

值得一提的是,我收到非常相似的错误消息,特别是:

注意:"A::A(("被隐式删除,因为默认定义格式不正确:class A : public Singleton<A>

显然,第二段代码无法编译,因为我们删除了默认构造函数并尝试在get_instance方法中将其与new T一起使用。

令我惊讶的是,第一个代码段也没有编译,带有类似的错误消息。链接的答案有错误吗?如何使用 CRTP 为单例实现通用基类/接口

以下是"现代化"代码段的模组:

template <class T>
class Singleton {
public:
Singleton& operator = (const Singleton&) = delete;
Singleton& operator = (Singleton&&)      = delete;
static T& get_instance() {
if(!instance)
instance = new T_Instance;
return *instance;
}
protected:
Singleton() {}
private:
struct T_Instance : public T {
T_Instance() : T() {}
};
static inline T* instance = nullptr;
};
class A : public Singleton<A> {
protected:
A() {}
};
int main()
{
auto& x = A::get_instance();
}

代码段中的更改摘要:

  • protected单例中的默认构造函数
  • private嵌套结构,用于访问派生类的受保护构造函数
  • protected派生类中的构造函数以防止实例化

此外,无需delete通过将默认 ctor 实现添加到 Singleton 类而隐式删除的构造函数。

不像 Richard Hodges 的例子那么小,但静态instance成员可以轻松添加用于自动化单元测试的 delete_instance(( 方法。

最小的可能(我认为(实现。

特征:

  • A 既不可复制、不可构造或可移动。(通过删除复制操作隐式删除移动运算符(
  • 实现的构造是线程安全的。
  • 在程序结束时确保销毁实施。

 

template <class T>
struct Singleton 
{
Singleton(const Singleton&)              = delete;
Singleton& operator = (const Singleton&) = delete;
static T& get_instance() {
static T _{allow()};
return _;
}
private:
struct allow {};
protected:
Singleton(allow) {}
};
class A: public Singleton<A> {
using Singleton<A>::Singleton;
//Rest of functionality for class A
};
int main()
{
auto& x = Singleton<A>::get_instance();
auto& y = A::get_instance();
// compiler error
auto z = A();
}

但是,为什么不将"单例性"作为实现细节呢?为什么用户需要知道对象是单例?

template <class T>
struct Singleton 
{
protected:
static T& get_impl() {
static T _;
return _;
}
};
// the real implementation of A
struct AImpl
{
void foo();
};
// A is a value-type which just happens to be implemented in terms of a
// single instance
struct A: public Singleton<AImpl> 
{
auto foo() { return get_impl().foo(); }
};
void bar(A a)
{
a.foo();
}
int main()
{
auto x = A();
x.foo();
auto y = A();
y.foo();
x = y;
bar(x);
}

稍后,如果您决定该类型不应是单例,则无需更改其接口(因此也不需要更改程序的其余部分(:

示例 - A 是单例,B 不是。接口是相同的。

#include <memory>
template <class T>
struct Singleton 
{
protected:
static T& get_impl() {
static T _;
return _;
}
};
template<class T>
struct CopyableIndirect
{
CopyableIndirect() = default;
CopyableIndirect(CopyableIndirect const& r)
: impl_(std::make_unique<T>(*r.impl_))
{
}
CopyableIndirect(CopyableIndirect&& r)
: impl_(std::move(r.impl_))
{
}
CopyableIndirect& operator=(CopyableIndirect const& r)
{
auto temp = r;
swap(temp);
return *this;
}
CopyableIndirect& operator=(CopyableIndirect && r)
{
auto temp = std::move(r);
swap(temp);
return *this;
}
void swap(CopyableIndirect& r)
{
std::swap(impl_, r.impl_);
}
protected:
T& get_impl() {
return *impl_;
}
T const& get_impl() const {
return *impl_;
}
std::unique_ptr<T> impl_ = std::make_unique<T>();
};
struct AImpl
{
void foo() const;
};
struct A: public Singleton<AImpl> 
{
auto foo() const { return get_impl().foo(); }
};
struct B: public CopyableIndirect<AImpl> 
{
auto foo() const { return get_impl().foo(); }
};
void bar(A const& a)
{
a.foo();
}
void bar(B const& a)
{
a.foo();
}
int main()
{
auto x = A();
x.foo();
auto y = B();
y.foo();
bar(x);
bar(y);
}

你的第一个代码块的问题Singleton(){}被标记为私有。 这意味着A无法访问它A因此不能是默认构造。 将构造函数protected将解决此问题

template <class ActualClass> 
class Singleton
{
public:
static ActualClass& GetInstance()
{
if(p == nullptr)
p = new ActualClass;
return *p; 
}
protected:
static ActualClass* p;
Singleton(){}
private:
Singleton(Singleton const &);
Singleton& operator = (Singleton const &); 
};
template <class T>
T* Singleton<T>::p = nullptr;
class A: public Singleton<A>
{
//Rest of functionality for class A
};
int main()
{
auto& x = Singleton<A>::GetInstance();
}

您的第二个代码 bock 也有类似的问题,但您没有private默认构造,而是将其标记为delete因此它不是 =default 可构造的,这意味着A也不是默认可构造的。 默认构造函数使其protected就像第一个示例一样将解决此问题

template <class T>
class Singleton {
public:
Singleton(const Singleton&)              = delete;
Singleton(Singleton&&)                   = delete;
Singleton& operator = (const Singleton&) = delete;
Singleton& operator = (Singleton&&)      = delete;
static T& get_instance() {
if(!instance)
instance = new T;
return *instance;
}
protected:
Singleton()                              = default;
static inline T* instance = nullptr;
};
class A: public Singleton<A> {
//Rest of functionality for class A
};
int main()
{
auto& x = Singleton<A>::get_instance();
}

最新更新