工业强度n吨基类模板 [英] Industrial-strength n-ton base class template

查看:165
本文介绍了工业强度n吨基类模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个n吨基类模板。我不担心懒惰,因此 是:


确保一个类只有n个实例


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

 模板< typename Derived,size_t n = 1> 
class n_ton_base //单例是默认的
{
static Derived instances [n +(n == 0)];
// Zerotons也支持
protected:

//阻止n_ton_base在继承层次结构之外使用
n_ton_base(){}

//防止复制n_ton_base(和Derived类)
n_ton_base(const n_ton_base&)= delete;

public:
//默认情况下获取第一个元素,对单例有用
template< size_t i = 0>
static Derived& get_instance()
{
static_assert(i return instances [i];
}
};

这里是如何使用它:

  class SingletonExample:public n_ton_base< SingletonExample> 
{
public:
void method()
{
std :: cout< 单身人士被过度使用。
}
};

class DoubletonExample:public n_ton_base< DoubletonExample,2>
{
public:
void method()
{
std :: cout< Doubleton<<这个<< hello.\\\
;
}
};

int main()
{
SingletonExample :: get_instance()。method();
DoubletonExample :: get_instance()。method();
DoubletonExample :: get_instance< 0>()。method();
DoubletonExample :: get_instance< 1>()。method();
}



不幸的是,代码不还编译:

  /tmp/ccsFtliS.o:在函数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的) :未定义的引用`n_ton_base< SingletonExample,1UL> ::情况下,
/tmp/ccsFtliS.o:在功能`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的) :未定义的引用`n_ton_base< DoubletonExample,2UL> ::情况下,
/tmp/ccsFtliS.o:在功能`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的) :未定义的引用`n_ton_base< DoubletonExample,2UL> ::情况下,
collect2:LD GAB 1 ALS恩德,状态zurück

我做了什么错


解决方案

作为的 Etienne Cordonnier指出了,使用本地静态而不是类static更容易:

 模板< typename Derived,size_t n = 1> 
class n_ton_base //单例是默认的
{
protected:

//阻止n_ton_base在继承层次结构之外使用
n_ton_base(){ }

//防止复制n_ton_base(和Derived类)
n_ton_base(const n_ton_base&)= delete;

public:
//默认情况下获取第一个元素,对单例有用
template< size_t i = 0>
static Derived& get_instance()
{
static_assert(i
static派生实例;
return instance;
}
};

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

这也可以实现线程安全延迟初始化,无需我做任何事情关于它。很好!


I am working on an n-ton base class template. I don't worry about laziness yet, so the Intent is:

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

Here is my code so far:

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

And here is how one would use it:

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

Unfortunately, the code doesn't compile yet:

/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

What did I do wrong?

解决方案

As Etienne Cordonnier pointed out, it is much easier to use a local static instead of a class static:

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

Note that each instantiated member function will have its own local static, so no array is needed.

This also achieves thread-safe lazy initialization without me having to do anything about it. Nice!

这篇关于工业强度n吨基类模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆