10

According to n4487 and other c++17 references, there will be new lambda function specifier - constexpr , which if present "explicitly specifies that the function call operator is a constexpr function.". I understand the motivation about constant expressions in lambdas. What is interesting for me is point 4 of proposal which states:

4) If the constexpr specifier is omitted within the lambda-declarator, the function call operator (or template) is constexpr if it would satisfy the requirements of a constexpr function.

This leads me to two questions:

  1. Why do we need constexpr specifier? Looks like that whether the lambda call operator will be constexpr or not depends only on the fact will it "satisfy the requirements of a constexpr function", but not from constexpr specifier presence.
  2. If it is acceptable to have constexpr lambda by default, why isn't it proposed for other types of functions as well - for example global functions? What will be the impact if compiler starts to treat all functions which cover requirements as constexpr?
aahzbg
  • 452
  • 3
  • 11

1 Answers1

7
  1. The constexpr qualifier makes it a compile error for the lambda to violate the requirements of constexpr functions. You use it when you explicitly need the lambda to be constexpr, so that you don't accidentally make it not constexpr.

  2. Asked and answered.

Community
  • 1
  • 1
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982