By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,915 Members | 1,362 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,915 IT Pros & Developers. It's quick & easy.

Friend function of template inner class?

P: n/a
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);

GCC compiles but complains at link time:
ZeroLink: unknown symbol '__ZeqRKN3FooIiE3BarES3_'

What am I doing wrong?
Sep 18 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Daniel T. 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 --
That's correct, the friend operator is not a member function, it is a
normal function with const Foo<T>::Bar& are parameters.

In these cases, it's much clearer to inline the operator. If the
operator is not trivial, have it call member functions.

--
Ian Collins.
Sep 18 '08 #2

P: n/a
On 9月18日, 下午12时10分, "Daniel T." <danie...@earthlink.netwrote:
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 it
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
Sep 18 '08 #3

P: n/a
Hello Barry, Where you get the C++ standard reference ?

Thanks for your help.
Sep 18 '08 #4

P: n/a
Pe********@gmail.com wrote:

Which reference? Keep the context you are replying to.
Hello Barry, Where you get the C++ standard reference ?
My guess would be from his copy of the standard...

--
Ian Collins.
Sep 18 '08 #5

P: n/a
>
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".
I looking for this reference. I guess this is a C++ standard
specification.

A billion thanks for your help.
Sep 20 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.