Given the following code :-
#include
#include
#include
#include
void func(std::function
Is this supposed to happen or just a deficiency in the compilers?
This is supposed to happen. std::function
has a constructor template that can take an argument of any type. The compiler can't know until after a constructor template is selected and instantiated that it's going to run into errors, and it has to be able to select an overload of your function before it can do that.
The most straightforward fix is to use a cast or to explicitly construct a std::function
object of the correct type:
func(std::function<void()>([](){}));
func(std::function<void(int)>([](int){}));
If you have a compiler that supports the captureless-lambda-to-function-pointer conversion and your lambda doesn't capture anything, you can use raw function pointers:
void func(void (*param)()) { }
void func(void (*param)(int)) { }
(It looks like you are using Visual C++ 2010, which does not support this conversion. The conversion was not added to the specification until just before Visual Studio 2010 shipped, too late to add it in.)
To explain the problem in a bit more detail, consider the following:
template <typename T>
struct function {
template <typename U>
function(U f) { }
};
This is basically what the std::function
constructor in question looks like: You can call it with any argument, even if the argument doesn't make sense and would cause an error somewhere else. For example, function<int()> f(42);
would invoke this constructor template with U = int
.
In your specific example, the compiler finds two candidate functions during overload resolution:
void func(std::function<void(void)>)
void func(std::function<void(int)>)
The argument type, some unutterable lambda type name that we will refer to as F
, doesn't match either of these exactly, so the compiler starts looking at what conversions it can do to F
to try and make it match one of these candidate functions. When looking for conversions, it finds the aforementioned constructor template.
All the compiler sees at this point is that it can call either function because
F
to std::function<void(void)>
using its converting constructor with U = F
and F
to std::function<void(int)>
using its converting constructor with U = F
.In your example it is obvious that only one of these will succeed without error, but in the general case that isn't true. The compiler can't do anything further. It has to report the ambiguity and fail. It can't pick one because both conversions are equally good and neither overload is better than the other.