I would like to be able to determine at compile time, taking into account the general type of lambda, whether it can be called with a given set of parameter types. I have the following C ++ 14 implementation example:
#include <iostream>
template <typename Func, typename... Args, typename = decltype(std::declval<Func>()(std::declval<Args>()...))>
auto eval(Func f, Args &&... args) { return f(args...); }
struct invalid_call { };
template <typename Func>
invalid_call eval(Func f, ...) { return invalid_call{}; };
template<class B>
struct negation : std::integral_constant<bool, !bool(B::value)> { };
template <typename Func, typename... Args>
using can_call = negation<std::is_same<decltype(eval(std::declval<Func>(), std::declval<Args>()...)), invalid_call>>;
struct foo {};
int main()
{
auto func = [](auto a1, auto a2) -> decltype(a1 + a2) { return a1 + a2; };
using FuncType = decltype(func);
std::cout << "can call with (int, int): " << can_call<FuncType, int, int>::value << std::endl;
std::cout << "can call with (foo, foo): " << can_call<FuncType, foo, foo>::value << std::endl;
}
This example works great as it is. What I am not is a cumbersome way to declare a lambda:
auto func = [](auto a1, auto a2) -> decltype(a1 + a2) { return a1 + a2; };
That is, the return type of the return must be specified, because C ++ 14 return types do not work with SFINAE . Returning the output type requires substituting the types of the argument list into the calling template call operator, and the program is poorly formed if an error occurs there.
Ideally, I could do the following:
auto func = [](auto a1, auto a2) { return a1 + a2; };
; . , decltype() , , . :
++ ( ++ 14, ), , ?