奇怪的重复模板 - 变化

Ghi*_*ita 8 c++ templates crtp

关于CRP如果我想实现它的轻微变化(使用模板模板参数),我得到一个编译错误:

template <template <typename T> class Derived>
class Base
{
public:
    void CallDerived()
    {
        Derived* pT = static_cast<Derived*> (this);
        pT->Action(); // instantiation invocation error here
    }
};

template<typename T>
class Derived: public Base<Derived>
{
public:
    void Action()
    {
    }
};
Run Code Online (Sandbox Code Playgroud)

我不确定会选择这个表单(不能为我编译)而不是使用它(这可行)

template <typename Derived>
class Base
{
public:
    void CallDerived()
    {
        Derived* pT = static_cast<Derived*> (this);
        pT->Action();
    }
};

template<typename T>
class Derived: public Base<Derived<T>>
{
public:
    void Action()
    {
    }
};
Run Code Online (Sandbox Code Playgroud)

Dmi*_*rov 11

这也应该编译.我们只需要明确指定其他模板参数

 template <typename T, template <typename T> class Derived>
 class Base
 {
 public:
     void CallDerived()
     {
        Derived<T>* pT = static_cast<Derived<T>*> (this);
        pT->Action(); // instantiation invocation error here
     }
 };

template<typename T>
class Derived: public Base<T,Derived>
{
public:
    void Action()
    {
    }
};
Run Code Online (Sandbox Code Playgroud)


Naw*_*waz 5

在第一个示例中,类模板实际上采用模板模板参数,而不仅仅是模板参数,如您所写:

template <template <typename T> class Derived>
class Base
{
     //..
};
Run Code Online (Sandbox Code Playgroud)

所以这段代码没有意义:

Derived* pT = static_cast<Derived*> (this);
pT->Action(); // instantiation invocation error here
Run Code Online (Sandbox Code Playgroud)

Derived是一个模板模板参数,需要您没有提供给它的模板参数.实际上,在CallDerived()函数中,您无法知道需要提供的类型,以便执行您想要执行的操作.

第二种方法是正确的解决方案.用它.