8

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

我不确定有人会选择这种形式(它不会为我编译)而不是使用它(这行得通)

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

2 回答 2

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()
    {
    }
};
于 2012-04-29T18:25:49.910 回答
5

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

template <template <typename T> class Derived>
class Base
{
     //..
};

所以这段代码没有意义:

Derived* pT = static_cast<Derived*> (this);
pT->Action(); // instantiation invocation error here

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

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

于 2012-04-29T18:19:14.350 回答