I'd like to do the following:
template <typename T> struct foo { template <typename S> friend struct foo<S>; private: // ... };
but my compiler (VC8) chokes on it:
error C3857: 'foo<T>': multiple template parameter lists are not allowed
I'd like to have all possible instantiations of template struct foo
friends of foo<T>
for all T
.
How do I make this work ?
EDIT: This
template <typename T> struct foo { template <typename> friend struct foo; private: // ... };
seems to compile, but is it correct ? Friends and templates have very unnatural syntax.
class B{ template<class V> friend int j(); } template<class S> g(); template<class T> class A { friend int e(); friend int f(T); friend int g<T>(); template<class U> friend int h(); };
You can template a struct as well as a class. However you can't template a typedef.
Friend Keyword in C++ But, to declare any class as a friend class, you do it with the friend keyword. You can use the friend keyword to any class to declare it as a friend class. This keyword enables any class to access private and protected members of other classes and functions.
A friend function is declared inside the class with a friend keyword preceding as shown below. class className{ …… friend returnType functionName(arg list); }; As shown above, the friend function is declared inside the class whose private and protected data members are to be accessed.
template<typename> friend class foo
this will however make all templates friends to each other. But I think this is what you want?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With