How to make parameterized Base a friend of Derived in CRTP?

487 views Asked by At

I would like to implement the CRTP on a parameterized Base, and make Base a friend of Derived:

template <template <typename> class Derived, class T>
class Base;

template <class T>
class Derived : public Base<Derived, T>
{
  friend class Base<Derived, T>;
};

I have a compilation error on VS2012 with the following message:

error C3200: 'Derived<T>' : invalid template argument for template parameter 'Derived', expected a class template

Thanks for your help.

1

There are 1 answers

1
Sebastian Redl On

Try this:

friend class Base<::Derived, T>;

If that doesn't work, your compiler doesn't support this form of friend declaration (it should, but what do I know), and you'll have to work around by extending the friendship to all Base instantiations.

template <template <typename> class D, class BT>
friend class Base;