As a general rule, explicit specialization is allowed on a namespace scope. The following code:
template<typename T>
class B {
template<typename U> void test(T) {};
template<> void test(int) {};
};
make g++ produce the following error:
error: explicit specialization in non-namespace scope
At the same time, friend function template specialization is one of two possible ways to make friend function templates work inside class templates. The following code works like a charm:
template<typename T>
class B;
template<typename T>
void test(B<T>);
template<typename T>
class B {
friend void test<>(B<T>);
};
At the same time, the following does not compile, giving the same error as for the Snippet one:
template<typename T>
class B {
template<typename U> friend void test(U);
template<>friend void test<B<T>>(B<T>);
};
My questions are:
- Why friends are getting this kind of special treatment (would appreciate a reference to a standard);
- What is this wicked sintax for full specialization anyways? Snippet:
friend void test<>(B<T>);
EDIT 1 Fixed syntax errors in snippet 1.