Home > IT, Programming > C++11: Lambdas and C-Function Pointers

C++11: Lambdas and C-Function Pointers

Just when you think you know everything about lambdas: This thread on comp.lang.c++.moderated made me realize, that lambdas do convert to old-school C-function pointers if their capture list is empty. Which means the following code compiles perfectly fine:

int (*funptr)(void) = []() -> int { return 42; };

Unfortunately, as soon as you do capture anything, be it by reference or by value, this will no longer work and you will have to rely on good-old std::function again.

Categories: IT, Programming
  1. No comments yet.
  1. No trackbacks yet.