【发布时间】:2015-11-24 05:26:37
【问题描述】:
#include <iostream>
using namespace std;
template <typename ReturnType, typename ArgumentType>
ReturnType Foo(ArgumentType arg){}
template <typename ArgumentType>
string Foo(ArgumentType arg) { cout<<"inside return 1"<<endl; return "Return1"; }
int main(int argc, char *argv[])
{
Foo<int>(2);
return 0;
}
以上代码抛出如下错误。
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>]
因为,函数重载只考虑函数名、参数类型列表和封闭的命名空间。为什么会抛出这个错误?
【问题讨论】:
标签: c++ function templates overloading