2

I have a legacy template function I'm trying to call that has a slew of specializations for function pointers with different numbers of arguments. I'm writing a new template function of my own that take some arbitrary kind of non-capturing lambda and needs to pass it to the other library. If I do this directly, then the template resolution fails. If however I explicitly cast it to be related function pointer type, things work.

The problem then is how to make my template code get that function pointer type from the lambda's type or force the explicit conversion without explicitly referencing the type.

1 Answer 1

8

Just use unary-plus to convert a lambda to a function pointer, as in:

 auto* f = +[]{ std::cout << "Hello, world!\n"; }; // f is of type void (*)()
Sign up to request clarification or add additional context in comments.

1 Comment

A slight more intuitive syntax (but less understandable as to why it works) option is *[]{...}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.