Templated derived class in CRTP (Curiously Recurring Template Pattern)
Asked Answered
L

2

7

I have a use of the CRTP that doesn't compile with g++ 4.2.1, perhaps because the derived class is itself a template? Does anyone know why this doesn't work or, better yet, how to make it work? Sample code and the compiler error are below.

Source: foo.C

#include <iostream>

using namespace std;

template<typename X, typename D> struct foo;

template<typename X> struct bar : foo<X,bar<X> >
{
  X evaluate() { return static_cast<X>( 5.3 ); }
};

template<typename X> struct baz : foo<X,baz<X> >
{
  X evaluate() { return static_cast<X>( "elk" ); }
};

template<typename X, typename D> struct foo : D
{
  X operator() () { return static_cast<D*>(this)->evaluate(); }
};

template<typename X, typename D>
void print_foo( foo<X,D> xyzzx )
{
  cout << "Foo is " << xyzzx() << "\n";
}

int main()
{
  bar<double> br;
  baz<const char*> bz;

  print_foo( br );
  print_foo( bz );

  return 0;
}

Compiler errors

foo.C: In instantiation of ‘foo<double, bar<double> >’:
foo.C:8:   instantiated from ‘bar<double>’
foo.C:30:   instantiated from here
foo.C:18: error: invalid use of incomplete type ‘struct bar<double>’
foo.C:8: error: declaration of ‘struct bar<double>’
foo.C: In instantiation of ‘foo<const char*, baz<const char*> >’:
foo.C:13:   instantiated from ‘baz<const char*>’
foo.C:31:   instantiated from here
foo.C:18: error: invalid use of incomplete type ‘struct baz<const char*>’
foo.C:13: error: declaration of ‘struct baz<const char*>’
Leesaleese answered 30/5, 2010 at 21:25 Comment(0)
H
4

The idea of CRTP is to have a base class that knows of what type its derivative is - not to let the base class derive from its derivative.
Otherwise you'd have the following situation:

  • Derived derives from Base<Derived>, which
  • derives from Derived, which
  • derives from Base<Derived>, which
  • ...

Use the following instead:

template<typename X, typename D> struct foo // : D
// ...                                         ^ remove that
Hinch answered 30/5, 2010 at 21:40 Comment(2)
Thanks, I don't know why I didn't catch that earlier.Leesaleese
@Butterwaffle, that's because :D - is very funny smile, you didn't want to delete it :DStagnate
H
5

There is a way to make CRTP works for a template derived class, with a caveat that it should always be a template.

#include <iostream>
#include <typeinfo>


template<template <class> class Derived, class T>
struct A{
    void interface(){
        static_cast<Derived<T>*>(this)->impl();
    }
};

template<class T>
struct B: public A<B, T>
{
    void impl(){
        std::cout << "CRTP with derived templates are real\n";
        std::cout << "Tempate argument is " << typeid(T).name() << "\n";
    }
};

int main(void)
{   
    B<char>().interface();
    B<double>().interface();
    B<void>().interface();
    return 0;
}

Works with gcc 4.4.7 and maybe older, can't check for sure.

Hellish answered 28/6, 2020 at 10:45 Comment(0)
H
4

The idea of CRTP is to have a base class that knows of what type its derivative is - not to let the base class derive from its derivative.
Otherwise you'd have the following situation:

  • Derived derives from Base<Derived>, which
  • derives from Derived, which
  • derives from Base<Derived>, which
  • ...

Use the following instead:

template<typename X, typename D> struct foo // : D
// ...                                         ^ remove that
Hinch answered 30/5, 2010 at 21:40 Comment(2)
Thanks, I don't know why I didn't catch that earlier.Leesaleese
@Butterwaffle, that's because :D - is very funny smile, you didn't want to delete it :DStagnate

© 2022 - 2024 — McMap. All rights reserved.