I am trying to make use of the Curiously Recurring Template Pattern, in which a base class template Base
is defined and a client derived type Derived
is expected to inherit from Base<Derived>
. I am finding that I am able to reference inner types of Base
without qualification from within Derived
when Derived
is not a template, but am unable to do so when Derived
is a template.
Below is a minimal example that fails to compile and demonstrates the problem.
namespace dtl
{
template <class Derived>
class Base
{
protected:
template <typename... Ts> struct A1 {};
struct A2 {};
};
} // namespace dtl
class Derived1 : public dtl::Base<Derived1>
{
struct B : A1<A2> {}; // works
};
template <int x>
class Derived2 : public dtl::Base<Derived2<x>>
{
struct B : A1<A2> {}; // fails
};
int main()
{
Derived1();
Derived2<0>();
}
Compiler-reported errors are not illuminating:
test2.cpp:22:18: error: expected template-name before ‘<’ token
22 | struct B : A1<A2> {}; // fails
| ^
test2.cpp:22:18: error: expected ‘{’ before ‘<’ token
test2.cpp:22:18: error: expected unqualified-id before ‘<’ token
Why does this problem occur, and what are the workarounds?