473,324 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Friend function of template inner class?

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
5 2431
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
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
Hello Barry, Where you get the C++ standard reference ?

Thanks for your help.
Sep 18 '08 #4
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
>
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Christophe Barbe | last post by:
I am not clear about friend functions of a template class. GCC (3.3.2) wants me to add <> after the friend function names in the class declaration and VisualC++ doesn't like that. template...
2
by: Christophe Barbe | last post by:
I posted a few days ago about the same problem but was not very clear. So here is my second take at it. Basically with GCC 3.3.2, I can't compile the example from the C++ FAQ Lite available...
1
by: Dmitry D | last post by:
Hi all, I'm having problems with declaring a template friend function. It seems like I've done everything as explained in C++ FAQ, but still, I'm getting the linker error (unresolved external...
5
by: Trevor Lango | last post by:
What is the appropriate syntax for placing a friend function that includes as one of it's parameters a pointer to the class object itself within the template class? I have the following: ...
6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
6
by: Adam Parkin | last post by:
Hello, all I'm having a problem with friend functions in a templatized Queue class I'm writing using linked lists. The problem is that I can't get the friend function to be able to access private...
5
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
4
by: fdmfdmfdm | last post by:
I have the following code: #include <iostream> #include <cstdlib> #include <cassert> using namespace std; template <class T> class Stack{ public: enum{DefaultStack = 10, EmptyStack = -1};
21
by: H9XLrv5oXVNvHiUI | last post by:
Hi, I have a question about injecting friend functions within template classes. My question is specific to gcc (version 3.4.5) used in combination with mingw because this code (or at least code...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.