Re: Friend function of template inner class?
On 9=D4 18=C8=D5, =CF =CE=E712=CA=B110=B7=D6, "Daniel T." <danie...@ear=
thlink.net> wrote:
The goal is to make a friend function of an inner template class...
template < typename T >
class Foo {
public:
class Bar {
friend bool operator==(
const typename Foo<T>::Bar& lhs, const typename Foo<T>::Bar& rhs);
};
};
template < typename T >
bool operator==(const typename Foo<T>::Bar& lhs, const typename
Foo<T>::Bar& rhs)
{
bool result = false;
return result;
}
#include <cassert>
int main()
{
Foo<int>::Bar a;
bool r = a == a;
assert( r == false );
}
Comeau says:
"bool operator==(const Foo<T>::Bar &, const Foo<T>::Bar &)" declares =
a
non-template function -- add <> to refer to a template instance
friend bool operator==(const typename Foo<T>::Bar& lhs, const
^
typename Foo<T>::Bar& rhs);
Actually, "Comeau Online" does NOT link. so it doesn't produce linkage
error
GCC compiles but complains at link time:
ZeroLink: unknown symbol '__ZeqRKN3FooIiE3BarES3_'
What am I doing wrong?
I. The easiest way to do this is define it inside of Foo::Bar;
....
....
friend operator== (Bar const& lhs, Bar const& rhs) { ... }
....
....
II. define it out side of Foo::Bar;
according to 14.5.3/1
your friend declaration of "operator==" is not a template function,
unless you add "<T>" after "operator==", meanwhile if you do this,
you have to forward declaring this templated "operator==", which also
needs you to forward declaring template class "Foo".
But in this case, you also get into the trouble of deducing T from
"bool r = a == a;" with a==Foo<int>::Bar, so IMHO, you can't do i=
t
in this way.
Besides, according how you declared friend "operator==" inside
Foo::Bar,
which was a non-template operator, you can specialize every T for
"operator=="
out side of Foo::Bar, like this:
bool operator== (Foo<int>::Bar const& lhs,
Foo<int>::Bar const& rhs)
{
...
}
--
Best Regards
Barry