1

In the code below I am having problems with conversion of lambda to template function pointer:

1) Non-template function f: Lambda conversion works fine.

2) Template function g: Lambda conversion requires specifying the template argument.

3) Variadic template function h: Even specifying the template argument doesn't work and I have to explicitly cast the lambda to function pointer.

void f(void(*)(int)) {}

template<class T> void g(void(*)(T)) {}

template<class... Ts> void h(void(*)(Ts...)) {}

int main()
{
    f([](int) {}); // OK: Lambda to function pointer conversion works fine

    g([](int) {}); // Error: cannot deduce template argument for void(*)(T) from lambda
    g<int>([](int) {}); // OK: Specifying the template argument works   

    h([](int) {}); // Error: cannot deduce template argument for void(*)(Ts...) from lambda
    h<int>([](int) {}); // Error: cannot deduce template argument for void(*)(int, Ts...) from lambda
    h(static_cast<void(*)(int)>([](int) {})); // OK: Explicit cast works
}

I'm Using MS VS 2015. I'm using case 3 heavily and while the cast works it clutters up the code and I'm curious as to why it is required and wondering if there's a better way.

George Skelton
  • 1,095
  • 1
  • 10
  • 22

0 Answers0