卡在模板需求循环中 [英] stuck in a template requirement loop

查看:262
本文介绍了卡在模板需求循环中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类使用附加模板添加如下的附加功能:

  template<类T> 
class AddOn_A
{
public:
int SomeFuncA()
{
T * pT = static_cast< T *>(这);
return pT-> DoSomething()+ 1;
};
};

class CMyClass:public AddOn_A< CMyClass>
{
public:
int DoSomething()
{
return 100;
};
};

int _tmain(int argc,_TCHAR * argv [])
{
CMyClass A;
_ASSERT(A.SomeFuncA()== 101);

return 0;
}



现在我想扩展这个CMyClass可以接受不同的加载项如AddOn_B。

 模板<类T> 
class AddOn_B
{
public:
int SomeFuncB()
{
T * pT = static_cast< T *>(这);
return pT-> DoSomething()+ 2;
};
};

template< class AddOn>
class CMyClass:public AddOn
{
public:
int DoSomething()
{
return 100;
};
};

int _tmain(int argc,_TCHAR * argv [])
{
//错误C3203:'AddOn_A':非特定类模板不能用作模板参数对于模板参数AddOn,期望一个真实类型
//错误C2955:'AddOn_A':使用类模板需要模板参数列表
CMyClass< AddOn_A>一个;
_ASSERT(A.SomeFuncA()== 101);

//这里有相同的错误
CMyClass< AddOn_B> B;
_ASSERT(B.SomeFuncB()== 102);

return 0;不幸的是,每个Add_On需要CMyClass作为模板参数,它需要一个Add_On等等。 ...我在一个需求循环。



有没有一些模板魔法,我可以用来获得我寻找的功能?有更好的方法吗?



谢谢,
PaulH

解决方案

显然,您尝试使用着名的奇怪的循环模板模式



我不知道你确实想要做什么,但你可能会得到另一个解决方案:



如果你使用了两个类,该怎么办:

  class Base {}; 

class MyClass:public AddOn< Base> {};

您也可以使用基于策略的方法:

  class PolicyA_A {}; 
class PolicyA_B {};

class PolicyB_A {};
class PolicyB_B {};

template< class PolicyA,class PolicyB>
class MyClass:private PolicyA,private PolicyB {};

typdef MyClass< PolicyA_A,PolicyB_A> MyClassAA;

这个想法是将工作的一部分委托给政策以增加灵活性。



最后但同样重要的是,您可以使用装饰方法:

  class Base {}; 

template< class T>
class AddOn_A:public T {};

class MyClass:public AddOn_A< AddOn_B < Base> > {};

它允许你通过抑制多重继承和使层次结构线性化来摆脱虚拟继承。


I have a class that uses an "add-on" template to add additional functionality as below:

template< class T >
class AddOn_A
{
public: 
    int SomeFuncA()
    {
        T* pT = static_cast< T* >( this );
        return pT->DoSomething() + 1;
    };
};

class CMyClass : public AddOn_A< CMyClass >
{
public:
    int DoSomething()
    {
        return 100;
    };
};

int _tmain(int argc, _TCHAR* argv[])
{
    CMyClass A;
    _ASSERT( A.SomeFuncA() == 101 );

    return 0;
}

Now I would like to extend this such that CMyClass can accept different add-ons like AddOn_B.

template< class T >
class AddOn_B
{
public: 
    int SomeFuncB()
    {
        T* pT = static_cast< T* >( this );
        return pT->DoSomething() + 2;
    };
};

template< class AddOn >
class CMyClass : public AddOn
{
public:
    int DoSomething()
    {
        return 100;
    };
};

int _tmain(int argc, _TCHAR* argv[])
{
    // error C3203: 'AddOn_A' : unspecialized class template can't be used as a template argument for template parameter 'AddOn', expected a real type
    // error C2955: 'AddOn_A' : use of class template requires template argument list
    CMyClass< AddOn_A > A;
    _ASSERT( A.SomeFuncA() == 101 );

    // same errors here
    CMyClass< AddOn_B > B;
    _ASSERT( B.SomeFuncB() == 102 );

    return 0;
}

Unfortunately, each Add_On requires CMyClass as a template parameter which requires an Add_On, etc... I'm in a requirement loop.

Is there some template magic I can use to get the functionality I'm looking for? Is there a better method of doing this?

Thanks, PaulH

解决方案

Apparently you are trying to use the famous Curiously Recurring Template Pattern.

I am not sure of what you exactly want to do, but you might get away with another solution:

What if you used two classes:

 class Base {};

 class MyClass: public AddOn<Base> {};

You may also use a Policy Based approach:

 class PolicyA_A {};
 class PolicyA_B {};

 class PolicyB_A {};
 class PolicyB_B {};

 template <class PolicyA, class PolicyB>
 class MyClass: private PolicyA, private PolicyB {};

 typdef MyClass<PolicyA_A, PolicyB_A> MyClassAA;

The idea is to delegate part of the job to policies to add flexibility.

Last but not least you may use a Decorator approach:

 class Base {};

 template <class T>
 class AddOn_A: public T {};

 class MyClass: public AddOn_A< AddOn_B< Base > > {};

It allows you to get rid of the virtual inheritance by suppressing the Multi Inheritance and making the hierarchy linear.

这篇关于卡在模板需求循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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