This actually isn't related to the accessibility and friendship, it's related to the use of CRTP. Consider the following example that also exhibits the problem:
template <class T>
struct Base
{
typedef typename T::Data Data;
};
struct ThisWontCompile : public Base<ThisWontCompile>
{
struct Data { };
};
The problem is that ThisWontCompile
is incomplete at the time it is used as a template argument to Base
, so it can only be used as an incomplete type in Base
.
For a handful of solutions to your specific problem, consult the answers to this other question, especially Martin's recommendation to use a traits class, which would basically look like this:
// Base
template <typename T>
struct BaseTraits;
template <typename T>
struct Base
{
typedef typename BaseTraits<T>::Data Data;
};
// Derived
struct Derived;
template <>
struct BaseTraits<Derived>
{
struct Data { };
};
struct Derived : public Base<Derived>
{
};
typename BaseTraits<Derived>::Data
can be used in both Derived
and in Base
. If Derived
is itself a template, you can use a partial specialization for the traits class.