Is it possible to declare an entire instance of a template class friendly?

Let's say I have a template (helper), and I want all the instance classes of the template to be friends (so I can hide some static member functions as private, even if they periodically change the template arguments inside).

Like this:

template </* some types */>
class Foo {
    template </* same as above types */>
    friend class Foo</* template arguments */>;
    // ...
};

However, this will not compile since gcc warns me that I specialize in some pattern that is not allowed (should appear in the namespace area). I'm not trying to specialize in anything ...

Is there any way to do this?


, , , . , , ( <>).

:

template <class... Ts>
friend class Foo<Ts...>;
+4
1

, . ( )

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
};
+9

Source: https://habr.com/ru/post/1663909/


All Articles