C++ 函数重载和模板 #包括 使用名称空间std; 模板 ReturnType Foo(ArgumentType arg){} 模板 字符串Foo(ArgumentType arg){cout

C++ 函数重载和模板 #包括 使用名称空间std; 模板 ReturnType Foo(ArgumentType arg){} 模板 字符串Foo(ArgumentType arg){cout,c++,function,templates,overloading,C++,Function,Templates,Overloading,对Foo(2)的调用可以是: In function 'int main(int, char**)': 34:18: error: call of overloaded 'Foo(int)' is ambiguous 34:18: note: candidates are: 7:12: note: ReturnType Foo(ArgumentType) [with ReturnType = int; ArgumentType = int] 20:8: note: std::string

Foo(2)
的调用可以是:

In function 'int main(int, char**)': 
34:18: error: call of overloaded 'Foo(int)' is ambiguous 
34:18: note: candidates are: 
7:12: note: ReturnType Foo(ArgumentType) [with ReturnType = int; ArgumentType = int] 
20:8: note: std::string Foo(ArgumentType) [with ArgumentType = int; std::string = std::basic_string<char>]

编译器无法判断您想要哪一个,因为它们都有相同的参数:

string Foo(ArgumentType arg); //with ArgumentType = int

但是该函数是用一个模板参数调用的,即模板函数
returntypefoo(ArgumentType arg){}
需要两个模板parameter@q126y它可以推断第二个参数是什么,因为它是参数的类型。好的,那么错误不应该与重载有关,因为导致错误的不是重载。@q126y,是函数重载。有两个函数模板名为
Foo
@james,但特殊的ized模板[导致错误]具有相同的函数名、参数列表和封闭的名称空间,因此不存在函数重载的情况。如果我错了,请更正。谢谢。
ReturnType Foo(ArgumentType arg); //with ReturnType = int, ArgumentType deduced as int
string Foo(ArgumentType arg); //with ArgumentType = int
int Foo(int);
string Foo(int);