Forget about your case. as that is too complex for analysis.
Take this simple example:
template<typename T>
struct X
{
X(T data) {}
};
template<typename T>
void f(X<T> x) {}
Now call f
as:
f(10);
Here you might be tempted to think that T
will be deduced to int
and therefore, the above function call should work. Well, that is not the case. To keep thing simple, imagine that there is another constructor which takes int
as:
template<typename T>
struct X
{
X(T data) {}
X(int data) {} //another constructor
};
Now what T
should be deduced to, when I write f(10)
? Well, T
could any type.
Note that there could be many other such cases. Take this specialization, for instance:
template<typename T>
struct X<T*> //specialized for pointers
{
X(int data) {};
};
Now what T
should be deduced to for the call f(10)
? Now it seems even harder.
It is therefore non-deducible context, which explains why your code doesn't work for std::function
which is an identical case — just looks complex at the surface. Note that lambdas are not of type std::function
— they're basically instances of compiler generated classes (i.e they're functors of different types than std::function
).
The issue is on the nature of lambdas. They are function objects with a fixed set of properties according to the standard, but they are not a function. The standard determines that lambdas can be converted into std::function<>
with the exact types of arguments and, if they have no state, function pointers.
But that does not mean that a lambda is a std::function
nor a function pointer. They are unique types implementing operator()
.
Type deduction, on the other hand, will only deduce exact types, with no conversions (other than const/volatile qualifications). Because the lambda is not a std::function
the compiler cannot deduce the type in the call: filter(mySet,[](int i) { return i%2==0; });
to be any std::function<>
instantiation.
As of the other examples, in the first one you convert the lambda to the function type, and then pass that. The compiler can deduce the type there, as in the third example where the std::function
is an rvalue (temporary) of the same type.
If you provide the instantiating type int
to the template, second working example, deduction does not come into play the compiler will use the type and then convert the lambda to the appropriate type.
If we have:
template <typename R, typename T>
int myfunc(std::function<R(T)> lambda)
{
return lambda(2);
}
int r = myfunc([](int i) { return i + 1; });
It will not compile. But if you previously declare:
template <typename Func, typename Arg1>
static auto getFuncType(Func* func = nullptr, Arg1* arg1 = nullptr) -> decltype((*func)(*arg1));
template <typename Func>
int myfunc(Func lambda)
{
return myfunc<int, decltype(getFuncType<Func, int>())>(lambda);
}
You can call your function with a lambda parameter without problem.
There are 2 new pieces of code here.
First, we have a function declaration which is only useful to return an old-style function pointer type, based on given template parameters:
template <typename Func, typename Arg1>
static auto getFuncType(Func* func = nullptr, Arg1* arg1 = nullptr) -> decltype((*func)(*arg1)) {};
Second, we have a function that takes a template argument to build our expected lambda type calling 'getFuncType':
template <typename Func>
int myfunc(Func lambda)
{
return myfunc<int, decltype(getFuncType<Func, int>())>(lambda);
}
With the correct template parameters, now we can call the real 'myfunc'. Complete code will be:
template <typename R, typename T>
int myfunc(std::function<R(T)> lambda)
{
return lambda(2);
}
template <typename Func, typename Arg1>
static auto getFuncType(Func* func = nullptr, Arg1* arg1 = nullptr) -> decltype((*func)(*arg1)) {};
template <typename Func>
int myfunc(Func lambda)
{
return myfunc<int, decltype(getFuncType<Func, int>())>(lambda);
}
int r = myfunc([](int i) { return i + 1; });
You can declare any overload for 'getFuncType' to match your lambda parameter. For example:
template <typename Func, typename Arg1, typename Arg2>
static auto getFuncType(Func* func = nullptr, Arg1* arg1 = nullptr, Arg2* arg2 = nullptr) -> decltype((*func)(*arg1, *arg2)) {};