在阅读this answer之后,我尝试了一些简单的CRTP用法。我想我会尝试实现Singleton(是的,我知道 - 它只是用于练习和研究)模式,因为链接的答案类型已经做到了......除了它不编译的事实。
引用的代码如下:
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>
。
显然,第二段代码无法编译,因为我们删除了默认构造函数并尝试在new T
方法中使用get_instance
。
让我感到惊讶的是,第一个片段也没有编译,具有类似的错误消息。链接的答案是否有错误?如何使用CRTP为Singletons实现通用基类/接口?
这是“现代化”片段的mod:
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
构造函数在派生类中防止实例化此外,不需要通过向Singleton类添加默认ctor实现来隐式删除的delete
构造函数。
不像Richard Hodges的例子那么小,但静态instance
成员可以很容易地添加一个delete_instance()方法用于自动化单元测试。
您的第一个代码块的问题是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 constructable意味着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();
}
最小可能(我认为)实施。
特征:
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();
}
但为什么不让'singleton-ness'成为一个实现细节呢?为什么用户需要知道对象是单例?
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);
}