Suppose we have the following code:
template<class T> struct S;
template<class T> void operator++(S<T>);
template<class T> struct S {
friend void operator++(S);
};
template<class T>
void operator++(S<T>) {}
int main() {
S<int> s;
++s;
}
This will compile but won't link, because the friend
declaration introduces a non-template operator++
, that has never been defined.
This FAQ answer reads (bold is mine):
The solution is to convince the compiler while it is examining the class body proper that the
operator++
function is itself a template. There are several ways to do this;
The first way is to add <>
into the friend declaration, and I'm not considering it here. The second is "to define the friend function within the class body":
template<class T> struct S {
friend void operator++(S) { }
};
The quote suggests that void operator++(S)
is now a function template and not a non-template function. Is it?