I'm trying to implement template function when return parameter is either void or T. I tried different variations of the code above using sfinae but still not sure if that is in general possible in case lamdba is function parameter. The following code does not compile :
#include <functional>
template <typename T>
T Apply(const std::function<T()>& func)
{
return func();
}
template <>
void Apply(const std::function<void()>& func)
{
func();
}
int main(int argc, char *argv[])
{
int i1 = Apply([]() { return 10; });
bool b1 = Apply([]() { return true; });
Apply([]() { return; });
return 0;
}
Error :
error C2672: 'Apply': no matching overloaded function found
error C2784: 'T Apply(const std::function<T(void)> &)': could not deduce template argument for 'const std::function<T(void)> &' from 'main::<lambda_536cc9cae26ef6d0d1fbeb7b66a2e26b>'
wandbox live