Home >Backend Development >C++ >Is a C 11 Lambda Expression's Type Deduced with 'auto' a Function Pointer?
Lambda Expression Type Deduction with "auto" in C 11
In C 11, lambdas are a powerful feature that allow programmers to define anonymous functions. By using the "auto" keyword to deduce the type of a lambda, a common misconception arises that lambdas are of function pointer type.
The Nature of Lamdas
However, this assumption is incorrect. The underlying type of a lambda expression is not explicitly defined. Instead, lambdas are typically syntactic sugar for functors. A lambda is directly translated into a functor object, where elements within the [] brackets become constructor parameters and object members, while parameters within the () brackets become parameters for the functor's operator().
Lambda Conversion to Function Pointers
Lambdas that capture no variables can indeed be converted to function pointers. This conversion is supported by the standard, but not by all compilers (e.g., MSVC2010). However, this conversion does not imply that the lambda's actual type is a function pointer.
Conclusion
The type of a lambda expression deduced with "auto" is an unspecified functor type. While lambdas that capture no variables can be converted to function pointers, it's important to understand that this conversion is not equivalent to their true underlying type.
The above is the detailed content of Is a C 11 Lambda Expression's Type Deduced with 'auto' a Function Pointer?. For more information, please follow other related articles on the PHP Chinese website!