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: ---
#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: ---