ambiguous functions

Status
Not open for further replies.

KaiWen

Usenet Archive
Joined
Nov 5, 2010
Messages
28
Reaction score
0
I test template overload, some codes:
#include <iostream>

template <unsigned int n>
struct int2Type {};

template <int times>
void foo(double val, int2Type<times>) {
std::cout << "will output " << val << " " << times << "times" << std::endl; }

template <int times>
void foo(double val, int2Type<0>) {
std::cout << "this is a overload test." << std::endl;
}

int main(void) {
foo<0>(3.14, int2Type<0>);

return 0;
}

gcc output:
|| g++ -Wall -g -std=gnu++0x -c test.cpp
|| test.cpp: In function ‘int main’:
test.cpp|17| error: call of overloaded ‘foo(double, int2Type<0u>)’ is ambiguous test.cpp|7| note: candidates are: void foo(double, int2Type<times>) \ [with int times = 0] test.cpp|12| note: void foo(double, int2Type<0u>) \ [with int times = 0] || make: *** [test.o] error 1

but when I change int2Type to below, it work.

template <int n>
struct int2Type {};

I don't know why, can you help me?

--- - complaints: ---
 

KaiWen

Usenet Archive
Joined
Nov 5, 2010
Messages
28
Reaction score
0
On 2011-01-13, KaiWen wrote:
template <int times>
void foo(double val, int2Type<0>) {
std::cout << "this is a overload test." << std::endl;
}

I just found that I delete the line:
template <int times>
it also work.

I know it need not be a template.
void foo(double val, int2Type<0>) {
std::cout << "this is a overload test." << std::endl;
}
is enough.

I feel stupid.

--- - complaints: ---
 

Johannes Schaub (litb

Usenet Archive
Joined
Nov 27, 2009
Messages
22
Reaction score
0
KaiWen wrote:

I test template overload, some codes:
#include <iostream>

template <unsigned int n>
struct int2Type {};

template <int times>
void foo(double val, int2Type<times>) {
std::cout << "will output " << val << " " << times << "times" < std::endl; }

template <int times>
...

That's because of 14.8.2.5/17 (in the n3225 draft). Deduction of int2Type<unique-time> against int2Type<0> will fail, int2Type<0> against int2Type<time> will fail likewise because of 14.8.2.5/17.

If you change "n" to "int", 14.8.2.5/17 will not apply anymore. Then, deduction of int2Type<0> against int2Type<time> will work, but int2Type<unique-time> against int2Type<0> will fail. Note that since "time" is not used in the parameter list to be compared, it not being deduced won't partial ordering to fail, because of 14.8.2.4/11.

(all my spec references ae against n3225, which contains a much more precise description of partial ordering than what c++03 contains).
 

KaiWen

Usenet Archive
Joined
Nov 5, 2010
Messages
28
Reaction score
0
On 2011-01-13, KaiWen wrote:
On 2011-01-13, KaiWen wrote:
> template <int times>
> void foo(double val, int2Type<0>) {
> std::cout << "this is a overload test." << std::endl;
> }

I just found that I delete the line:
template <int times>
it also work.

I know it need not be a template.
void foo(double val, int2Type<0>) {
...

but these are ambiguous too:
#include <iostream>

template <int n>
struct int2Type {};

template <unsigned int times, class T>
void foo(T v, int2Type<times>) {
std::cout << "will output " << v << " for " << times << " times" << std::endl; }

template <unsigned int times, class T>
void foo(T v, int2Type<0>) {
std::cout << "this is template overload test." << std::endl; }

int main(void) {
// foo<3>(3.14, int2Type<3>);
foo<0>(3.14, int2Type<0>);

return 0;
}

囧

--- - complaints: ---
 

KaiWen

Usenet Archive
Joined
Nov 5, 2010
Messages
28
Reaction score
0
On 2011-01-13, Johannes Schaub (litb) wrote:
That's because of 14.8.2.5/17 (in the n3225 draft). Deduction of int2Type<unique-time> against int2Type<0> will fail, int2Type<0> against int2Type<time> will fail likewise because of 14.8.2.5/17.

If you change "n" to "int", 14.8.2.5/17 will not apply anymore. Then, deduction of int2Type<0> against int2Type<time> will work, but int2Type<unique-time> against int2Type<0> will fail. Note that since "time" is not used in the parameter list to be compared, it not being deduced won't partial ordering to fail, because of 14.8.2.4/11.

(all my spec references ae against n3225, which contains a much more precise description of partial ordering than what c++03 contains).

Thanks very much! Can you give me a url? Thanks again.

--- - complaints: ---
 

Paul (archive)

Usenet Archive
Joined
Jan 19, 2000
Messages
2,241
Reaction score
0
"KaiWen" wrote:

I test template overload, some codes:
#include <iostream>

template <unsigned int n>
struct int2Type {};

template <int times>
void foo(double val, int2Type<times>) {
std::cout << "will output " << val << " " << times << "times" < std::endl; }

...
Tried changing the literal 0 to a positive integer literal?

Templates can be very confusing if you aren't zoned into that way of thinking. It's like a whole differnet programming language on its own. There used to be many template inconsistencies across compilers, I don't know it this is still the case.
 

SG

Usenet Archive
Joined
Dec 10, 1999
Messages
838
Reaction score
0
On 13 Jan., 07:08, KaiWen wrote:
I just found that I delete the line:
template <int times>
it also work.

I know it need not be a template.
void foo(double val, int2Type<0>) {
        std::cout << "this is a overload test." << std::endl;}

is enough.

Overload resolution prefers non-templates over function templates when there would otherwise be a tie (or something along these lines).
 
Status
Not open for further replies.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,245
Members
248,133
Latest member
DujonT

Latest Threads

Top