473,802 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A short question about resolving Overloaded Function Templates

In the code below, shouldn't the function call minimum(a,a); result in
compilation error? I read in Lippman's C++ Primer 3rd Edition on page
521, the call should be ambiguous.

But on g++ (versions 3.2.3 and 3.4.2) it compiles fine and the result
shows that minimum(a,a) is resolved using minimum(T,T)

I also tried it on http://www.comeaucomputing.com/tryitout/ and got
the
same result.

So what is the correct behaviour according to the standard?

I tried reading overloading resolution rules from a few sources
(including C++ Templates and online IBM C++ reference) but I still
cannot figure out.

So can anybody tell me what the correct behaviour is according to the
latest standard? If the correct behaviour is to choose minimum(T,T)
over minimum(T,U) in this case, what's the rule which determines this
behaviour? Has it anything to do with Partial Ordering rules?

Thank you very much in advance.
#include <iostream>
using std::cout;
using std::endl;

template <class T>
T minimum(T,T)
{ cout << "minimum(T, T)" << endl; }

template <class T, class U>
int minimum(T, U)
{ cout << "minimum(T, U)" << endl; }

int main()
{
int a = 10;
unsigned int b = 20;
minimum(a,b);
minimum(a,a); // shouldn't this call ambiguous?
}
Jul 22 '05 #1
3 1449

"CoolPint" <co******@yahoo .co.uk> wrote in message
news:15******** *************** ***@posting.goo gle.com...
In the code below, shouldn't the function call minimum(a,a); result in
compilation error? I read in Lippman's C++ Primer 3rd Edition on page
521, the call should be ambiguous.

But on g++ (versions 3.2.3 and 3.4.2) it compiles fine and the result
shows that minimum(a,a) is resolved using minimum(T,T)

I also tried it on http://www.comeaucomputing.com/tryitout/ and got
the
same result.

So what is the correct behaviour according to the standard?

I tried reading overloading resolution rules from a few sources
(including C++ Templates and online IBM C++ reference) but I still
cannot figure out.

So can anybody tell me what the correct behaviour is according to the
latest standard? If the correct behaviour is to choose minimum(T,T)
over minimum(T,U) in this case, what's the rule which determines this
behaviour? Has it anything to do with Partial Ordering rules?

Thank you very much in advance.
#include <iostream>
using std::cout;
using std::endl;

template <class T>
T minimum(T,T)
{ cout << "minimum(T, T)" << endl; }

template <class T, class U>
int minimum(T, U)
{ cout << "minimum(T, U)" << endl; }

int main()
{
int a = 10;
unsigned int b = 20;
minimum(a,b);
minimum(a,a); // shouldn't this call ambiguous?
}


Did you try 'double a'?
Jul 22 '05 #2
CoolPint wrote in news:15******** *************** ***@posting.goo gle.com in
comp.lang.c++:
In the code below, shouldn't the function call minimum(a,a); result in
compilation error? I read in Lippman's C++ Primer 3rd Edition on page
521, the call should be ambiguous.
#include <iostream>
using std::cout;
using std::endl;

template <class T>
T minimum(T,T)
{ cout << "minimum(T, T)" << endl; }

template <class T, class U>
int minimum(T, U)
{ cout << "minimum(T, U)" << endl; }

int main()
{
int a = 10;
unsigned int b = 20;
minimum(a,b);
minimum(a,a); // shouldn't this call ambiguous?
}


No, but there is a rule that when two or more function templates are
selected they can be disambiguated by effectivly "selecting the most
specialized".

See 14.5.5.2 Partial ordering of function templates, for the details.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
> No, but there is a rule that when two or more function templates are
selected they can be disambiguated by effectivly "selecting the most
specialized".

See 14.5.5.2 Partial ordering of function templates, for the details.

Rob.


Thank you for the reply. I found out why from GotW#49.

So it looks like the book (C++ Primer 3rd edition, page 521) has an
error unless I misinterprested the content.

I tried looking at the errata for the book, but I didn't find one
about it.

Thank you again.
Jul 22 '05 #4

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

Similar topics

15
1907
by: AMT2K5 | last post by:
Hello folks, I seem to have recieved a segfault within my function but am unsure on how to resolve it. I understand that it means that somewhere something is trying to access memory that is not prohibited or not available. The following function is an overloaded += operator that reads in strings in a record format seperated by comma delimiters except for the last data which terminates with a semicolon. I am adding these records to an...
9
2536
by: Gomaw Beoyr | last post by:
Two question about the "partial classes" (in the next wersion of ..NET). Question 1 ========== Will partial classes (in the next version of C#) have to be declared "partial" in ALL places. I.e. do we have to need to write:
10
1870
by: andrew browning | last post by:
i have overlaoded all of my arithmetic operators but all are functioning as multiplication. below is a sample of the addition operator: Rational operator + (const Rational& r1, const Rational& r2){ //Postcondition: sum of r1 and r2 are returned int numerator; int denominator;
75
5490
by: Steven T. Hatton | last post by:
No, this is not a troll, and I am not promoting Java, C-flat, D, APL, Bash, Mathematica, SML, or LISP. A college teacher recently posted to this newsgroup regarding her observation that there has been a significant decline in the number of students opting to take courses in C++. I just want to let people know what I believe are the biggest obstacles to C++ language acquisition, and what aspects of the language make it less appealing than...
2
3892
by: desktop | last post by:
If a function should work with different types you normally overload it: void myfun(int a){ // do int stuff } void myfun(std::string str){ // do string stuff }
1
1117
by: callmeastha | last post by:
hey guys i am studying c++ and i'd be thankfl if u cud anwer these qs for me 1. a template can be considered as a kind of a macro. then wat is d difference between them? 2.diff. btwn overloaded function and function templates thnx.......
8
1882
by: kevin | last post by:
Hello! So I was reading O'Reilly's C++ in a Nutshell when I came accross something interesting: The class definition for basic_ostream contains numerous overloaded operator<< functions: template <class charT, class traits = char_traits<charT class basic_ostream : virtual public basic_ios<charT,traits>
5
1894
by: Lars Hillebrand | last post by:
Hello, i discovered a weird behaviour if i use templates together with virtual inheritance and method over. I managed to reproduce my problem with a small example: // *********** <code example********** template<typename Tclass TypedInterface { public: virtual void TestFunction( T * ) = 0;
3
6410
by: mathieu | last post by:
Could someone please tell me what is wrong with the following -ugly- piece of c++ code. Why when I explicititely set the template parameter my gcc compiler start getting confused: bla.cxx: In function 'int main()': bla.cxx:25: error: call of overloaded 'foo(short unsigned int*&)' is ambiguous bla.cxx:2: note: candidates are: void foo(OutputType*) bla.cxx:10: note: void foo(PixelType*)
0
10536
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
10304
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...
0
9114
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
7598
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
6838
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
5494
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...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
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
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.