Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curiously recurring template - variation

Regarding CRP if I want to implement a slight variation of it (using template template parameter) I get a compile error:

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

I am not exactly sure one would chose this form (that does not compile for me) instead of using this though (this works)

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()
    {
    }
};
like image 518
Ghita Avatar asked Apr 29 '12 18:04

Ghita


2 Answers

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()
    {
    }
};
like image 186
Dmitri Chubarov Avatar answered Sep 27 '22 18:09

Dmitri Chubarov


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.

like image 24
Nawaz Avatar answered Sep 27 '22 18:09

Nawaz