I'm wondering if anyone knows why the following sample doesn't compile giving an ambiguous call to overload function error. If I replace the auto with a strongly typed functor signature, it then is able to properly distinguish between the two method overloads.
I noticed the same issue doesn't occur when not using std::function as my overload arguments. If my overloads take just a simple float and int, the compiler can properly distinguish between the two overloads even when using the auto keyword to define my input arguments. I'm compiling this in VisualStudio 2012. Could this just be an bug in the VS compiler? I don't have access to a machine with GCC or Clang on it right now, but does anyone know if this would compile there?
Compile Error: ambiguous call to overload function
class AmbiguousOverload
{
public:
static void OverloadedMethod(std::function<int()>) {}
static void OverloadedMethod(std::function<float()>) {}
};
int _tmain(int argc, _TCHAR* argv[])
{
auto func1 = []() -> float {
return 0.5f;
};
auto func2 = []() -> int {
return 12;
};
AmbiguousOverload::OverloadedMethod(func1);
AmbiguousOverload::OverloadedMethod(func2);
return 0;
}
Compiles
class AmbiguousOverload
{
public:
static void OverloadedMethod(std::function<int()>) {}
static void OverloadedMethod(std::function<float()>) {}
};
int _tmain(int argc, _TCHAR* argv[])
{
std::function<float()> func1 = []() -> float {
return 0.5f;
};
std::function<int()> func2 = []() -> int {
return 12;
};
AmbiguousOverload::OverloadedMethod(func1);
AmbiguousOverload::OverloadedMethod(func2);
return 0;
}
Also Compiles
class AmbiguousOverload
{
public:
static void OverloadedMethod(int) {}
static void OverloadedMethod(float) {}
};
int _tmain(int argc, _TCHAR* argv[])
{
auto v1 = 0.5f;
auto v2 = 12;
AmbiguousOverload::OverloadedMethod(v1);
AmbiguousOverload::OverloadedMethod(v2);
return 0;
}
See Question&Answers more detail:os