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

template overload resolution

given the code snippet;

template<typename T>
void foo(T,T){}

template<typename T1,typename T2>
void foo(T1*,T2*){}

int main( ) {

foo((int*)0,(int*)0);

}

Could anyone please explain me why the second template is not more
viable for overload resolution?
AFAIK each argument is matched with corresponding parameter. in that
case shouldnt T1* and T2* match better than T for (int*)? I assume T1
and T2 are disjoint aren't they? or must they be different types?

Thanks in advance.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 31 '07 #1
8 1863
hurcan solter wrote:
given the code snippet;

template<typename T>
void foo(T,T){}

template<typename T1,typename T2>
void foo(T1*,T2*){}

int main( ) {

foo((int*)0,(int*)0);

}

Could anyone please explain me why the second template is not more
viable for overload resolution?
AFAIK each argument is matched with corresponding parameter. in that
case shouldnt T1* and T2* match better than T for (int*)? I assume T1
and T2 are disjoint aren't they? or must they be different types?
A template with fewer arguments is considered more specialised than one
with more arguments, I guess. The more arguments a template has, the
more generic it is.

Those are just guesses, of course. I don't have time to look through
the Standard for confirmation.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 31 '07 #2
A template with fewer arguments is considered more specialised than one
with more arguments, I guess. The more arguments a template has, the
more generic it is.

Those are just guesses, of course. I don't have time to look through
the Standard for confirmation.
Well, in fact the call is ambiguous.it's unambiguous if i define
foo(T*,T*)
which suggests that,there is also a relation between argument that it
prevents from
one preferred over another.

Jul 31 '07 #3
hurcan solter <hs*****@gmail.comwrote:
given the code snippet;

template<typename T>
void foo(T,T){}

template<typename T1,typename T2>
void foo(T1*,T2*){}

int main( ) {

foo((int*)0,(int*)0);

}

Could anyone please explain me why the second template is not more
viable for overload resolution?
[...]
From <http://www.comeaucomputing.com/tryitout/>:

Comeau C/C++ 4.3.9 (Mar 27 2007 17:24:47) for ONLINE_EVALUATION_BETA1
Copyright 1988-2007 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions

"ComeauTest.c", line 9: error: more than one instance of overloaded
function "foo"
matches the argument list, the choices that match are:
function template "void foo(T, T)"
function template "void foo(T1 *, T2 *)"
The argument types that you used are: (int *, int *)
foo((int*)0,(int*)0);
^

1 error detected in the compilation of "ComeauTest.c".
Thanks in advance.
HTH,

Schobi

--
Sp******@gmx.de is never read
I'm HSchober at gmx dot de
"If there were some arcane way to remove the heads of every
newsgroup troll on the planet, I think it would elevate
humans to a whole new level of intelligence."
Rocky Frisco

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 31 '07 #4
Hendrik Schober wrote:
hurcan solter <hs*****@gmail.comwrote:
>given the code snippet;

template<typename T>
void foo(T,T){}

template<typename T1,typename T2>
void foo(T1*,T2*){}

int main( ) {

foo((int*)0,(int*)0);

}

Could anyone please explain me why the second template is not more
viable for overload resolution?
[...]

From <http://www.comeaucomputing.com/tryitout/>:

Comeau C/C++ 4.3.9 (Mar 27 2007 17:24:47) for ONLINE_EVALUATION_BETA1
Copyright 1988-2007 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions

"ComeauTest.c", line 9: error: more than one instance of overloaded
function "foo"
matches the argument list, the choices that match are:
function template "void foo(T, T)"
function template "void foo(T1 *, T2 *)"
The argument types that you used are: (int *, int *)
foo((int*)0,(int*)0);
^

1 error detected in the compilation of "ComeauTest.c".
> Thanks in advance.

HTH,

Schobi
Could you perhaps enlighten us how this explains *why* the second
template is *not* more viable?

Thanks!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 31 '07 #5
Victor Bazarov <v.********@comAcast.netwrote:
Hendrik Schober wrote:
hurcan solter <hs*****@gmail.comwrote:
given the code snippet;
>
template<typename T>
void foo(T,T){}
>
template<typename T1,typename T2>
void foo(T1*,T2*){}
>
int main( ) {
>
foo((int*)0,(int*)0);
>
}
>
[...]

From <http://www.comeaucomputing.com/tryitout/>:

Comeau C/C++ 4.3.9 (Mar 27 2007 17:24:47) for ONLINE_EVALUATION_BETA1
Copyright 1988-2007 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions

"ComeauTest.c", line 9: error: more than one instance of overloaded
function "foo"
matches the argument list, the choices that match are:
function template "void foo(T, T)"
function template "void foo(T1 *, T2 *)"
The argument types that you used are: (int *, int *)
foo((int*)0,(int*)0);
^

1 error detected in the compilation of "ComeauTest.c".
Thanks in advance.
HTH,

Schobi

Could you perhaps enlighten us how this explains *why* the second
template is *not* more viable?
I don't think I can.
I /guessed/ neither specialization to be more viable.
I tried Comeau and found it agreeing with my guess.
So I posted Comeau's opinion on the matter. (When I
did this, my news server didn't have your reply yet,
so I wasn't trying to contradict you.)

Schobi

--
Sp******@gmx.de is never read
I'm HSchober at gmx dot de
"If there were some arcane way to remove the heads of every
newsgroup troll on the planet, I think it would elevate
humans to a whole new level of intelligence."
Rocky Frisco
Jul 31 '07 #6
Hendrik Schober wrote:
Victor Bazarov <v.********@comAcast.netwrote:
>Hendrik Schober wrote:
>>hurcan solter <hs*****@gmail.comwrote:
given the code snippet;

template<typename T>
void foo(T,T){}

template<typename T1,typename T2>
void foo(T1*,T2*){}

int main( ) {

foo((int*)0,(int*)0);

}
[...]
>>>
From <http://www.comeaucomputing.com/tryitout/>:

Comeau C/C++ 4.3.9 (Mar 27 2007 17:24:47) for
ONLINE_EVALUATION_BETA1 Copyright 1988-2007 Comeau Computing. All
rights reserved. MODE:strict errors C++ C++0x_extensions

"ComeauTest.c", line 9: error: more than one instance of overloaded
function "foo"
matches the argument list, the choices that match are:
function template "void foo(T, T)"
function template "void foo(T1 *, T2 *)"
The argument types that you used are: (int *, int *)
foo((int*)0,(int*)0);
^

1 error detected in the compilation of "ComeauTest.c".

Thanks in advance.

HTH,

Schobi

Could you perhaps enlighten us how this explains *why* the second
template is *not* more viable?

I don't think I can.
I /guessed/ neither specialization to be more viable.
I tried Comeau and found it agreeing with my guess.
So I posted Comeau's opinion on the matter. (When I
did this, my news server didn't have your reply yet,
so I wasn't trying to contradict you.)
Please do contradict, I am all for a healthy discussion. Besides, my
answer was just a guess, I am not even sure it is correct unless it's
confirmed or proven wrong.

The reason I am asking is that it seems that your reply is essentially
giving the answer "because Comeau C++ says it is" to the 'why' question.
My understanding is that the OP had already found out that the second
function template was not more viable, you simply confirmed what the OP
already knew, *OR* I misunderstood something (which happens more often
as I grow older, unfortunately), and then I need to be shown the light.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 31 '07 #7
Victor Bazarov <v.********@comAcast.netwrote:
Hendrik Schober wrote:
Victor Bazarov <v.********@comAcast.netwrote:
Hendrik Schober wrote:
hurcan solter <hs*****@gmail.comwrote:
given the code snippet;
>
template<typename T>
void foo(T,T){}
>
template<typename T1,typename T2>
void foo(T1*,T2*){}
>
int main( ) {
>
foo((int*)0,(int*)0);
>
}
>
[...]

From <http://www.comeaucomputing.com/tryitout/>:

Comeau C/C++ 4.3.9 (Mar 27 2007 17:24:47) for
ONLINE_EVALUATION_BETA1 Copyright 1988-2007 Comeau Computing. All
rights reserved. MODE:strict errors C++ C++0x_extensions

"ComeauTest.c", line 9: error: more than one instance of overloaded
function "foo"
matches the argument list, the choices that match are:
function template "void foo(T, T)"
function template "void foo(T1 *, T2 *)"
The argument types that you used are: (int *, int *)
foo((int*)0,(int*)0);
^

1 error detected in the compilation of "ComeauTest.c".

Thanks in advance.

HTH,

Schobi
>
Could you perhaps enlighten us how this explains *why* the second
template is *not* more viable?
I don't think I can.
I /guessed/ neither specialization to be more viable.
These aren't specializations, but overloaded
functions. (No partial specialization of
function templates. I know. Damn.)
I tried Comeau and found it agreeing with my guess.
So I posted Comeau's opinion on the matter. (When I
did this, my news server didn't have your reply yet,
so I wasn't trying to contradict you.)

Please do contradict, I am all for a healthy discussion. Besides, my
answer was just a guess, I am not even sure it is correct unless it's
confirmed or proven wrong.
My answer was just a guess, tooI wouldn't know chapter
and verse, but this

template<typename T>
void foo(T,T){}

template<typename T1,typename T2>
void foo(T1*,T2*){}

int main()
{
foo((int*)0,(int*)0);
}

looks to me as if neither overloaded version is
a better fit than the other -- which is why I
asked Comeau to confirm. .
The reason I am asking is that it seems that your reply is essentially
giving the answer "because Comeau C++ says it is" to the 'why' question.
My understanding is that the OP had already found out that the second
function template was not more viable, you simply confirmed what the OP
already knew, *OR* I misunderstood something (which happens more often
as I grow older, unfortunately), and then I need to be shown the light.
I'm sorry I wasn't making my opinion clearer. I
assumed that neither function template was a
better fit. I thought posting an error message
that says exactly that would make that clear. I
should probably have been more explicit, though.
V
Schobi

--
Sp******@gmx.de is never read
I'm HSchober at gmx dot de
"If there were some arcane way to remove the heads of every
newsgroup troll on the planet, I think it would elevate
humans to a whole new level of intelligence."
Rocky Frisco
Jul 31 '07 #8
On Aug 1, 2:19 am, hurcan solter <hsol...@gmail.comwrote:
template<typename T>
void foo(T,T){}

template<typename T1,typename T2>
void foo(T1*,T2*){}

int main( ) {

foo((int*)0,(int*)0);

}

Could anyone please explain me why the second template is not more
viable for overload resolution?
AFAIK each argument is matched with corresponding parameter. in that
case shouldnt T1* and T2* match better than T for (int*)? I assume T1
and T2 are disjoint aren't they? or must they be different types?
As far as the compiler is concerned, both can be matched equally well.
No conversions are involved in either case, so there is no reason for
the compiler to favour one over the other. You might be getting
confused with *class* partial specialization. Eg
#include <iostream>

template<typename T>
class Bar
{
public:
Bar() { std::cout << "(1)" << std::endl; }
};

template<typename T>
class Bar<T*>
{
public:
Bar() { std::cout << "(2)" << std::endl; }
};

int main()
{
Bar<int barInt; // Prints (1)
Bar<int* barIntPtr; // Prints (2)
}

--
Computational Modeling, CSIRO (CMIS)
Melbourne, Australia
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Aug 1 '07 #9

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

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
2
by: Asfand Yar Qazi | last post by:
Hi, If I do this: void func(const int& i) {...} template<class T> func(const T& i) {...}
3
by: Ruben Campos | last post by:
Greetings. Some time ago, I had writing a CVector <T, N> class, which implements an algebraic vector of arbitrary both dimension and scalar type. First, I defined the interface for the generic...
5
by: Andreas Mueller | last post by:
Hi All, I have an overloaded generic method anmed "Foo": Class Test Class Xox(Of T) End Class Class Lulli(Of T) Inherits Xox(Of T) End Class
6
by: merdem | last post by:
Hi all, I just started to mess around with templates. First I declared a class Image as follows(this is a small version of the real thing which is pretty big): template<int depth, int space,...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
16
by: xman | last post by:
I defined namespace hpc in main.cpp, so not to clash with other libraries. But I found that, in namespace boo, instantiating a template with a class in namespace hpc, causes compilation errors by...
9
by: neildferguson | last post by:
I am using templates with a little project I am working on. My compiler (GCC) is finding a particular construct ambiguous. Can anyone suggest something I might change in the declaration of class...
5
by: C++Liliput | last post by:
Consider the following code template<class T1, class T2> T2 sum(T1 a, T1 b) { T2 ret = a + b; return ret; } int main()
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: 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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...

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.