Home > Backend Development > C++ > Is a C 11 Lambda Expression's Type Deduced with 'auto' a Function Pointer?

Is a C 11 Lambda Expression's Type Deduced with 'auto' a Function Pointer?

Linda Hamilton
Release: 2024-12-16 04:20:12
Original
754 people have browsed it

Is a C  11 Lambda Expression's Type Deduced with

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template