Skip to content

Latest commit

 

History

History
53 lines (42 loc) · 1.6 KB

lambda-expressions-constexpr.md

File metadata and controls

53 lines (42 loc) · 1.6 KB
descriptiontitlems.datehelpviewer_keywordsms.assetid
Learn more about: constexpr lambda expressions in C++
constexpr lambda expressions in C++
04/08/2019
lambda expressions [C++], constexpr
b56346cd-fbff-475f-aeaa-ed2010c6d6f7

constexpr lambda expressions in C++

Visual Studio 2017 version 15.3 and later (available in /std:c++17 mode and later): A lambda expression may be declared as constexpr or used in a constant expression when the initialization of each data member that it captures or introduces is allowed within a constant expression.

int y = 32; auto answer = [y]() constexpr { int x = 10; return y + x; }; constexprintIncrement(int n) { return [n] { return n + 1; }(); }

A lambda is implicitly constexpr if its result satisfies the requirements of a constexpr function:

auto answer = [](int n) { return32 + n; }; constexprint response = answer(10);

If a lambda is implicitly or explicitly constexpr, and you convert it to a function pointer, the resulting function is also constexpr:

auto Increment = [](int n) { return n + 1; }; constexprint(*inc)(int) = Increment;

See also

C++ Language Reference
Function Objects in the C++ Standard Library
Function Call
for_each

close