工业强度的 n 吨基类模板
Industrial-strength n-ton base class template
本文关键字:基类 更新时间:2023-10-16
我正在研究一个n吨基类模板。我还不担心懒惰,所以意图是:
确保类只有 n 个实例,并提供对它们的全局访问点。
这是我到目前为止的代码:
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];
}
};
以下是如何使用它:
class SingletonExample : public n_ton_base<SingletonExample>
{
public:
void method()
{
std::cout << "Singletons are overused.n";
}
};
class DoubletonExample : public n_ton_base<DoubletonExample, 2>
{
public:
void method()
{
std::cout << "Doubleton " << this << " says hello.n";
}
};
int main()
{
SingletonExample::get_instance().method();
DoubletonExample::get_instance().method();
DoubletonExample::get_instance<0>().method();
DoubletonExample::get_instance<1>().method();
}
不幸的是,代码尚未编译:
/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
我做错了什么?
正如 Etienne Cordonnier 指出的那样,使用局部静态比使用静态类要容易得多:
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;
}
};
请注意,每个实例化的成员函数都有自己的本地静态函数,因此不需要数组。
这也实现了线程安全的延迟初始化,而无需我对此执行任何操作。好!
在全局范围内添加以下内容:
template<typename Derived, size_t n>
Derived n_ton_base<Derived, n>::instances[n + (n == 0)];
顺便说一句,std::array<>
允许零大小的数组,所以你可能要考虑一下。
你可以在这里找到一个关于类静态成员的解释:
静态数据成员(仅限C++):
类的成员列表中静态数据成员的声明不是定义。必须在命名空间范围内定义类声明之外的静态成员。例如:
class X { public: static int i; }; int X::i = 0; // definition outside class declaration
因此,您必须在类之外定义n_ton::instances
。
相关文章:
- std::具有相同基类的类的变体
- 是否可以初始化不可复制类型的成员变量(或基类)
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 基类中的函数名称解析
- C++初始化基类
- 如何通过派生类函数更改基类中的向量
- 如何定义一个纯抽象基类
- 如何使用基类指针引用派生类成员
- 继承:构造函数,初始化C++11中基类的类C数组成员
- 使用基类指针创建对象时,缺少派生类析构函数
- 如何引用基类的派生类?
- 如果基类包含双指针成员,则派生类的构造函数
- 在模板基类中为继承类中的可选重写生成虚拟方法
- 为什么此派生对象无法访问基类的后递减方法?
- 公开最直接的基类模板名称
- 当基类是依赖类型时,这是一个缺陷吗
- 如何使基类的运算符对基类的可变参数数可见(请参阅下面的代码)?
- 模板基类中的静态变量
- C++ 继承:将子类传递给需要基类的函数并获取子类行为
- 继承和友元函数,从基类访问受保护的成员