M
matthias.neubauer
I have problems understanding how overloading of function templates
works.
Consider first the following code without any function templates ...
int foo(const char *& c)
{
return 0;
}
int foo(const char *c)
{
return 1;
}
int main() {
const char * s = "Hi!";
return foo(s);
}
Overloading resolution of foo is ambiguous, as can be witness by e.g. g
++
Foo.cpp: In function 'int main()':
Foo.cpp:14: error: call of overloaded 'foo(const char*&)' is ambiguous
Foo.cpp:2: note: candidates are: int foo(const char*&)
Foo.cpp:7: note: int foo(const char*)
If I now use function templates for foo, foo_t, as follows
template<typename C>
int foo_t(C& c)
{
return 0;
}
template<typename C>
int foo_t(const C *c)
{
return 1;
}
int main() {
const char * s = "Hi!";
return foo_t(s);
}
some implementations I tested (g++, msvc) seem to find a best
candidate function for foo_i which I do not understand.
As I read 13.3.1 para 7 of the standard, I would expect an
implementation to first determine specializations for foo_t (here:
foo_t<const char *> and foo_t<char> resp.), and then proceed as above.
Hence, I'd also expect an ambiguity error here. Where is my
misunderstanding?
Cheers,
Matthias
works.
Consider first the following code without any function templates ...
int foo(const char *& c)
{
return 0;
}
int foo(const char *c)
{
return 1;
}
int main() {
const char * s = "Hi!";
return foo(s);
}
Overloading resolution of foo is ambiguous, as can be witness by e.g. g
++
Foo.cpp: In function 'int main()':
Foo.cpp:14: error: call of overloaded 'foo(const char*&)' is ambiguous
Foo.cpp:2: note: candidates are: int foo(const char*&)
Foo.cpp:7: note: int foo(const char*)
If I now use function templates for foo, foo_t, as follows
template<typename C>
int foo_t(C& c)
{
return 0;
}
template<typename C>
int foo_t(const C *c)
{
return 1;
}
int main() {
const char * s = "Hi!";
return foo_t(s);
}
some implementations I tested (g++, msvc) seem to find a best
candidate function for foo_i which I do not understand.
As I read 13.3.1 para 7 of the standard, I would expect an
implementation to first determine specializations for foo_t (here:
foo_t<const char *> and foo_t<char> resp.), and then proceed as above.
Hence, I'd also expect an ambiguity error here. Where is my
misunderstanding?
Cheers,
Matthias