473,701 Members | 2,918 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why overloading isn't happened?

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.

=============== =============== =============== =============== ==
Question 1:
=============== =============== =============== =============== ==

Compile the following codes, and run it.
The program will print

"T<a, b>"

on the screen.

However, I consider that "T<a, 3>" should be displayed.

Why this program print "T<a, b>"?

My building environment is:

1) gcc-3.3
2) linux kernel 2.6.6

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

#include <iostream>

template<int a, int b>
class T;

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

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

int c;

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)
{
t->c = 2;
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;
}

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

I add another function template:

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

and define it, too.

However, when I compile this program, gcc shows the following error
message:

eee.cpp: In instantiation of `T<2, 3>':
eee.cpp:50: instantiated from here
eee.cpp:19: error: ambiguous template specialization `func<3>' for
`void
func(const T<3, 3>*)'

What does the 'ambiguous template specialization' indicate?
I can't figure it out.

Thank you for your help.

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

#include <iostream>

template<int a, int b>
class T;

template<int a, int b>
void func(T<a, b> const * const t)
{
t->c = 3;
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 line -->

public:

void ttt() const;
};

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

template<int b> <-- add definition -->
void
func(T<3, b> const * const t)
{
t->c = 1;
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;
}

=============== =============== =============== =============== ==
Jul 22 '05 #1
3 2090
Yueh-Wei Hu wrote:
I have 2 questions about template function as friends in template
classes. I don't know why, and hope someone could help me.

=============== =============== =============== =============== ==
Question 1:
=============== =============== =============== =============== ==

Compile the following codes, and run it.
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>
The program will print

"T<a, b>"

on the screen.

However, I consider that "T<a, 3>" should be displayed.
When the program is corrected, yes.

Why this program print "T<a, b>"?
Because you have a buggy compiler, probably...

My building environment is:

1) gcc-3.3
2) linux kernel 2.6.6
Fall back on gcc-3.2

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

#include <iostream>

template<int a, int b>
class T;

template<int a, int b>
void func(T<a, b> const * const t)
{
t->c = 3;
'*t' is const here. Friendship doesn't change that. Comment it out.
std::cerr << "T<a, b>" << std::endl;
}

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

int c;

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)
{
t->c = 2;
Again, '*t' is a const object. Comment this out too.
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;
}
After commenting out the offending lines, the code compiles fine (as it
should).

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

I add another function template:

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

and define it, too.

However, when I compile this program, gcc shows the following error
message:

eee.cpp: In instantiation of `T<2, 3>':
eee.cpp:50: instantiated from here
eee.cpp:19: error: ambiguous template specialization `func<3>' for
`void
func(const T<3, 3>*)'

What does the 'ambiguous template specialization' indicate?
I can't figure it out.
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>
Thank you for your help.
[...]


Victor
Jul 22 '05 #2
Victor Bazarov <v.********@com Acast.net> wrote in message news:<AG******* *********@dfw-read.news.verio .net>...
=============== =============== =============== =============== ==
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 #3
Yueh-Wei Hu <yw**@iii.org.t w> wrote...
Victor Bazarov <v.********@com Acast.net> wrote in message news:<AG******* *********@dfw-read.news.verio .net>... =============== =============== =============== =============== ==
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:
[...]

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


You need to talk to people in a gnu newsgroup. Apparently gcc is
still behind on the standard compliancy. The explanation is simple:
the compiler is not up to par. The solution is simpler: use another
[better] compiler. If you can't, you're screwed until they manage
to fix it.

Victor
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
4717
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP manual mentions "overloading" (http://no.php.net/manual/en/language.oop5.overloading.php), but it isn't really overloading at all... Not in the sense it's used in other languages supporting overloading (such as C++ and Java). As one of the...
15
24003
by: Susan Baker | last post by:
Hello everybody, I'm new to C++ (I have some C background). I've read up on this topic a few times but it just dosen't seem to be sinking in. 1. Whats the difference between overloading and overriding? 2. When is one preferable to use as opposed to the other? 3. How are virtual functions related to this topic (overloading/overriding) - a real world example of using virtual functions would be very much appreciated.
16
16277
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
39
2192
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any C implementation supporting this feature? I assume some of you will claim that there is no need in function overloading, so I would like to know your arguments too. Thanks,
45
3278
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
31
2283
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
14
1734
by: Klaus Löffelmann | last post by:
Hi, does anybody know, why the second contructor isn't called in this example? Are you able to reproduce this bug? Thanks Klaus Public Class Something
15
2778
by: lordkain | last post by:
is it possible to do some kind of function overloading in c? and that the return type is different
11
28135
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
5
5417
by: ashu | last post by:
i have studied this example of overloading operator, but i don`t know exactly what happened in code .kindly explain me: #include <iostream> using namespace std; const int SIZE = 3; class atype { int a; public: atype() {
0
8649
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9232
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9084
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8978
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7827
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6573
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5905
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4411
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3104
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.