Industrial-strength n-ton base class template
我正在开发一个n-ton基类模板。我还不担心懒惰,所以目的是:
Ensure a class has only n instances, and provide a global point of access to them.
这是迄今为止我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | template<typename Derived, size_t n = 1> class n_ton_base // Singletons are the default { static Derived instances[n + (n == 0)]; // Zerotons are supported, too protected: // Prevent n_ton_base to be used outside of inheritance hierarchies n_ton_base() {} // Prevent n_ton_base (and Derived classes) from being copied n_ton_base(const n_ton_base&) = delete; public: // Get first element by default, useful for Singletons template<size_t i = 0> static Derived& get_instance() { static_assert(i < n,"Time to increase n it seems!"); return instances[i]; } }; |
下面是如何使用它:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | class SingletonExample : public n_ton_base<SingletonExample> { public: void method() { std::cout <<"Singletons are overused. "; } }; class DoubletonExample : public n_ton_base<DoubletonExample, 2> { public: void method() { std::cout <<"Doubleton" << this <<" says hello. "; } }; int main() { SingletonExample::get_instance().method(); DoubletonExample::get_instance().method(); DoubletonExample::get_instance<0>().method(); DoubletonExample::get_instance<1>().method(); } |
不幸的是,代码还没有编译:
1 2 3 4 5 6 7 | /tmp/ccsFtliS.o: In function `SingletonExample& n_ton_base<SingletonExample, 1ul>::get_instance<0ul>()': nton.cpp:(.text._ZN10n_ton_baseI16SingletonExampleLm1EE12get_instanceILm0EEERS0_v[SingletonExample& n_ton_base<SingletonExample, 1ul>::get_instance<0ul>()]+0x5): undefined reference to `n_ton_base<SingletonExample, 1ul>::instances' /tmp/ccsFtliS.o: In function `DoubletonExample& n_ton_base<DoubletonExample, 2ul>::get_instance<0ul>()': nton.cpp:(.text._ZN10n_ton_baseI16DoubletonExampleLm2EE12get_instanceILm0EEERS0_v[DoubletonExample& n_ton_base<DoubletonExample, 2ul>::get_instance<0ul>()]+0x5): undefined reference to `n_ton_base<DoubletonExample, 2ul>::instances' /tmp/ccsFtliS.o: In function `DoubletonExample& n_ton_base<DoubletonExample, 2ul>::get_instance<1ul>()': nton.cpp:(.text._ZN10n_ton_baseI16DoubletonExampleLm2EE12get_instanceILm1EEERS0_v[DoubletonExample& n_ton_base<DoubletonExample, 2ul>::get_instance<1ul>()]+0x5): undefined reference to `n_ton_base<DoubletonExample, 2ul>::instances' collect2: ld gab 1 als Ende-Status zurück |
我做错了什么?
正如EtienneCordonnier指出的,使用局部静态而不是类静态更容易:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | template<typename Derived, size_t n = 1> class n_ton_base // Singletons are the default { protected: // Prevent n_ton_base to be used outside of inheritance hierarchies n_ton_base() {} // Prevent n_ton_base (and Derived classes) from being copied n_ton_base(const n_ton_base&) = delete; public: // Get first element by default, useful for Singletons template<size_t i = 0> static Derived& get_instance() { static_assert(i < n,"Time to increase n it seems!"); static Derived instance; return instance; } }; |
注意,每个实例化的成员函数都有自己的局部静态,因此不需要数组。
这还实现了线程安全的延迟初始化,而不需要我做任何事情。好极了!
在全局范围中添加:
1 2 | template<typename Derived, size_t n> Derived n_ton_base<Derived, n>::instances[n + (n == 0)]; |
顺便说一句,
您可以在这里找到关于类静态成员的解释:
Static data members (C++ only):
The declaration of a static data member in the member list of a class is not a definition. You must define the static member outside of the class declaration, in namespace scope. For example:
1
2
3
4
5
6 class X {
public:
static int i;
};
int X::i = 0; // definition outside class declaration
因此,您必须在类外定义