Say I have a template (helper) class, and I want to make all the instantiated classes of the template to be friend (so I can hide some static member function as private, even if they switch template arguments occasionally inside).
Like this:
template </* some types */>
class Foo {
template </* same as above types */>
friend class Foo</* template arguments */>;
// ...
};
However, this won't compile since gcc warns me that I'm specializing some template which is not allowed (must appear at namespace scope). I'm not trying to specializing anything...
Is there any way to do it?
Originally, since there are many many arguments, I was trying to use variadic template to save some typing, but that's considered specialization by compiler. Though later I switched back to type all arguments, the explicit specialization is invoked (the <>
is kept).
The very original code:
template <class... Ts>
friend class Foo<Ts...>;
Yes you can, just use the correct template friends declaration syntax. e.g. (explanations are written in the comments)
template <T>
class Foo {
// declare other instantiations of Foo to be friend
// note the name of the template parameter should be different with the one of the class template
template <typename X>
friend class Foo; // no </* template arguments */> here, otherwise it would be regarded as template specialization
};
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