Why is C++ lambda implemented with functor instead of function pointer?

c++, lambda

Solution

The problem is that a lambda function in C++ can have an additional state (captured variables aka context) which has to be passed around for each instance (they can differ for each instance of the handle to the same lambda function).

A function cannot have a state coupled to the handle you pass around. If you would add such a state to a function pointer you end up writing a wrapper which needs to be callable using the parenthesis syntax (`operator()`) which happens to be what a functor is.

A notable fact is that a lambda without a capture can be converted to a function pointer. This is only possible because it does not require such additional space.

Problem

I found out that lambdas in both MSVC and GCC are functors implementing an `operator()`. What is the reason they prefer functor to function pointers?

Original source