简体   繁体   中英

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

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM