Curiously recurring template - variation

走远了吗. 提交于 2019-12-03 03:13:38

This should compile as well. We just need to get the other template parameter specified explicitly

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

In the first example, the class template actually takes template template parameter, not just template parameter, as you've written:

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

So this code doesn't make sense:

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

Here Derived is a template template argument which needs template argument which you didn't provided to it. In fact, in the CallDerived() function, you cannot know the type you need to provide to it, in order to do what you intend to do.

The second approach is the correct solution. Use it.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!