Re: How can I declare and define a friend template function in a template class?
hurcan solter wrote:
You can't redefine 'T' here, I think. If you only wanted to make
the 'index<T>' a friend (instantiated on the same 'T' as the class
'Test'), then a pair of angle brackets is in order (I missed that
in my post:
template<typename T>
class Test {
public:
friend Test<T> index<>(T start, T end, T step);
// ^^^^
};
yes you can. it's so called friend template, in that case,all
instantiantions of the
template is the friend of class Test.Not that it's useful in this case
though, so yours
is the probably way to go.
I repeat, the name 'T' shall not be redeclared in the definition of
'Test' for any other purpose. Try this:
template<class T> class A {
A(T) {}
template<class T> friend void foo(T t);
// template<class U> friend void foo(U u);
};
template<class T> void foo(T t)
{
A<A<T> > aat(A<int>(42));
}
int main()
{
foo(0);
}
Then, after it fails, comment the 'foo(T t)' declaration in the
class template, and uncomment the 'foo(U u)' declaration and try
again.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask