Home  >  Article  >  Backend Development  >  Can Lambda Functions Be Used in `constexpr` Expressions?

Can Lambda Functions Be Used in `constexpr` Expressions?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-01 01:15:28456browse

Can Lambda Functions Be Used in `constexpr` Expressions?

Constexpr Support for Lambda Functions

Original Question:

Can lambda functions utilize the constexpr keyword?

Response:

In C 14, lambda functions are not supported by constexpr. However, this is set to change with the adoption of N4487. This proposal would permit lambdas and specific closure type operations in constant expressions.

Explanation:

Under [expr.const]/(2.6) of the C 14 standard, lambdas are excluded from constant expressions. This limitation is outlined in N4487:

"This proposal suggests allowing **lambda-expressions** in constant expressions, removing an existing restriction. The authors propose that certain **lambda-expression**s and operations on certain closure objects be allowed to appear within constant expressions."

In addition, the proposal states that:

  • Closure types are considered literal types if their data members are all literal types.
  • If the constexpr specifier is omitted from the lambda declarator, the generated function call operator becomes constexpr as long as it meets the requirements of a constexpr function. This behavior is similar to constexpr inference for implicitly defined constructors and assignment operators.

The above is the detailed content of Can Lambda Functions Be Used in `constexpr` Expressions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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