gpt4 book ai didi

c++ - 工业级n吨基类模板

转载 作者:可可西里 更新时间:2023-11-01 18:10:57 27 4
gpt4 key购买 nike

我正在研究一个 n-ton 基类模板。我还不担心懒惰,所以 Intent 是:

Ensure a class has only n instances, and provide a global point of access to them.

到目前为止,这是我的代码:

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 pointed out ,使用局部静态而不是类静态要容易得多:

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;
}
};

请注意,每个实例化的成员函数都有自己的局部静态,因此不需要数组。

这也实现了thread-safe lazy initialization无需我做任何事情。不错!

关于c++ - 工业级n吨基类模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14662673/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com