473,320 Members | 1,947 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,320 software developers and data experts.

using friend function template in class template

Victor Bazarov <v.********@comAcast.net> wrote in message news:
================================================== ============
Question 1:
================================================== ============
Your code refuses to compile. In function 'foo<a>' 't' is a pointer to
const. Friendship has nothing to do with it. When I remove the attempt
to change 't->c' in foo<a>, it compiles and runs and displays

T<a, 3>

This is my fault, sorry.

The 't->c' assignment is added by me after I copied and pasted the
source codes to the news.

The reason why I did this is to emphasize the need of 'friend'
declarations.
Fall back on gcc-3.2
I fall back to gcc-3.2, and I can _not_ compile it !

The error message from gcc-3.2 is as following:

================================================== ============

eee.cpp: In member function `void T<a, b>::ttt() const [with int a =
2, int b =
3]':
eee.cpp:52: instantiated from here
eee.cpp:44: call of overloaded `func(const T<2, 3>* const)' is
ambiguous
eee.cpp:8: candidates are: void func(const T<a, b>*) [with int a = 2,
int b =
3]
eee.cpp:29: void func(const T<a, 3>*) [with int a = 2]
eee.cpp:8: void func(const T<a, b>*) [with int a = 2,
int b =
3]
eee.cpp:29: void func(const T<a, 3>*) [with int a = 2]

================================================== ============

The source code is as following:

================================================== ============

#include <iostream>

template<int a, int b>
class T;

template<int a, int b>
void func(T<a, b> const * const t)
{
std::cerr << "T<a, b>" << std::endl;
}

template<int a, int b>
class T
{
private:

friend void func<a, b>(T<a, b> const * const t);
friend void func<a>(T<a, 3> const * const t);

public:

void ttt() const;
};

template<int a>
void
func(T<a, 3> const * const t)
{
std::cerr << "T<a, 3>" << std::endl;
}
template<int a, int b>
void
T<a, b>::ttt() const
{
func(this);
}

int
main()
{
T<2, 3> t;

t.ttt();

return 0;
}

================================================== ============

However, when I use gcc-3.2, gcc-3.3, gcc-3.4 to compile my code, the
result is strange.

Here is my result:

1) gcc-3.2: can _not_ compile, the error message is copied and pasted
above.

2) gcc-3.3: can compile, but the result is

T<a, b>

3) gcc-3.4: can compile, but the result is

T<a, b>

So....
What behavior conforms to the C++ standard?
And which version of gcc is correct?

================================================== =========== Question 2:
================================================== ===========


Neither can I. The code compiles file once the offending lines that
attemp to change 't->c' are commented out and the resulting program outputs

T<a, 3>
Victor


I can _not_ compile the code using gcc-3.2, and the error message is
like the one I posted above:

================================================== ============

eee.cpp: In instantiation of `T<2, 3>':
eee.cpp:50: instantiated from here
eee.cpp:19: ambiguous template specialization `func<3>' for `void
func(const
T<3, 3>*)'
eee.cpp: In member function `void T<a, b>::ttt() const [with int a =
2, int b =
3]':
eee.cpp:52: instantiated from here
eee.cpp:44: call of overloaded `func(const T<2, 3>* const)' is
ambiguous
eee.cpp:8: candidates are: void func(const T<a, b>*) [with int a = 2,
int b =
3]
eee.cpp:29: void func(const T<a, 3>*) [with int a = 2]
eee.cpp:8: void func(const T<a, b>*) [with int a = 2,
int b =
3]
eee.cpp:29: void func(const T<a, 3>*) [with int a = 2]

================================================== ============

The source code I use is as following:

================================================== ============

#include <iostream>

template<int a, int b>
class T;

template<int a, int b>
void func(T<a, b> const * const t)
{
std::cerr << "T<a, b>" << std::endl;
}

template<int a, int b>
class T
{
private:

friend void func<a, b>(T<a, b> const * const t);
friend void func<a>(T<a, 3> const * const t);
friend void func<b>(T<3, b> const * const t); <-- add this -->

public:

void ttt() const;
};

template<int a>
void
func(T<a, 3> const * const t)
{
std::cerr << "T<a, 3>" << std::endl;
}

template<int b> <-- add this definition -->
void
func(T<3, b> const * const t)
{
std::cerr << "T<3, b>" << std::endl;
}

template<int a, int b>
void
T<a, b>::ttt() const
{
func(this);
}

int
main()
{
T<2, 3> t;

t.ttt();

return 0;
}

================================================== ============

Again, the following is the result when I compile it using gcc-3.2,
gcc-3.3 and gcc-3.4:

1) gcc-3.2: can _not_ compile, the error message posted above.
2) gcc-3.3: can _not_ compile, the error message just like the one
posted for gcc-3.2.
3) gcc-3.4: can compile, but the result is

T<a, b>

================================================== ============

I can not figure it out why overload doesn't appear.
Could somebody explain this?

Thank you for you help.

Jul 22 '05 #1
0 1668

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: ...
3
by: 胡岳偉(Yueh-Wei Hu) | last post by:
Hi all, I have 2 questions about template function as friends in template classes. I don't know why, and hope someone could help me. ...
8
by: Douglas | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** Hello, The following code does not compile if line 3 is uncommented "using namespace std". I do not understand it. Could...
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:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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...
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)...
0
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...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.