This seems to work:
template<class A> struct S {
template<class B> friend S<B> f(B);
};
template<class B> S<B> f(B) {return S<B>{};}
int main() {
f(5);
}
OK, so let’s do a seemingly purely cosmetic change and move the definition of f
to the body of the struct
:
template<class A> struct S {
template<class B> friend S<B> f(B) {return S<B>{};}
};
int main() {
f(5);
}
Suddenly compilation starts failing:
prog.cpp: In function ‘int main()’:
prog.cpp:6:5: error: ‘f’ was not declared in this scope
f(5);
^
Why does a template friend function need to be defined outside of the class to work in this snippet?
Is there any trick that would allow function f
to be defined within the body of the class definition?