When using the curiously recurring template pattern, I am unable to refer to typedefs belonging to the derived class only if I attempt to reference them from the base class; gcc complains no type named 'myType' in class Derived<...>
. This seems inconsistent with what is otherwise possible using typedefs, templates, and curiously recurring relationships.
Consider:
/* crtp.cpp */
#include <iostream>
using namespace std;
// case 1. simple.
class Base {
public:
typedef int a_t;
a_t foo;
};
class Derived : public Base {
a_t bar;
};
// case 2. template.
template<typename T>
class tBase {
public:
typedef T b_t;
T foo;
};
template <typename T>
class tDerived : public tBase<T> {
typename tBase<T>::b_t bar;
};
// case 3. curiously recurring.
template <typename T, typename D>
class tCuriousBase {
public:
typedef T c_t;
c_t foo;
};
template <typename T>
class tCuriousDerived : public tCuriousBase<T,tCuriousDerived<T> > {
typename tCuriousBase<T,tCuriousDerived<T> >::c_t bar;
};
// case 4. curiously recurring with member reference.
template <typename T, typename D>
class tCuriousMemberBase {
public:
T foo;
T get() {
return static_cast<D*>(this)->bar;
}
};
template <typename T>
class tCuriousMemberDerived : public tCuriousMemberBase<T, tCuriousMemberDerived<T> > {
public:
T bar;
tCuriousMemberDerived(T val) : bar(val) {}
};
// case 5. curiously recurring with typedef reference.
template <typename T, typename D>
class tCuriousTypeBase {
public:
typedef T d_t;
d_t foo;
typename D::c_t baz;
};
template <typename T>
class tCuriousTypeDerived : public tCuriousTypeBase<T, tCuriousTypeDerived<T> > {
public:
typedef T c_t;
typename tCuriousTypeBase<T,tCuriousTypeDerived<T> >::d_t bar;
};
// entry point
int main(int argc, char **argv) {
Derived::a_t one = 1;
tDerived<double>::b_t two = 2;
tCuriousDerived<double>::c_t three = 3;
double four = tCuriousMemberDerived<double>(4).get();
tCuriousTypeBase<double, tCuriousDerived<double> >::d_t five = 5;
// tCuriousTypeDerived<double>::d_t six = 6; /* FAILS */
cout << one << endl;
cout << two << endl;
cout << three << endl;
cout << four << endl;
cout << five << endl;
// cout << six << endl;
}
From (1), we see that typedefs are indeed inherited from base to derived; a typedef declared in the base class can be accessed through the derived class.
From (2), we see that this is still true if both classes are templates.
From (3), we see that this typedef inheritance can still exist in the presence of a curiously recurring template relationship; we refer to the base's typedef via the derived class in our declaration of three
.
From (4), we see that member variables of the derived class may be readily accessed from the base class.
From (5), we see that we can access a typedef defined on a template parameter (this works when we declare five
using types that are not related by inheritance).
As soon as we make the template parameter a derived class in (6), however, suddenly the typedef becomes inaccessible, even though it is seemingly as well-defined as any member variable in the derived class.
Why is this?