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

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 1435

"CoolPint" <co******@yahoo.co.uk> wrote in message
news:15**************************@posting.google.c om...
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.google.c om 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
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...
9
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. ...
10
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&...
75
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...
2
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
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...
8
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: ...
5
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...
3
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.