/*constexpr lambda is a C++ feature that allows you to define a lambda function that
can be evaluated at compile-time, if the arguments passed to it are also known at 
compile-time. This is useful when you need to do some computation at compile-time that
can't be done with a regular constexpr function or variable.

In this example, the lambda add is defined as a constexpr lambda, which means it
can be used in both constant expression and non-constant expression contexts. The
lambda takes two int arguments and returns their sum.

In the main function, we first use the lambda in a constant expression context by
assigning the result of add(3, 4) to a constexpr variable result. Then, we use the
lambda in a non-constant expression context by assigning the result of add(x, y) to
a non-constexpr variable sum.

The constexpr keyword on the lambda indicates that the lambda can be evaluated at
compile-time if all its arguments are known at compile-time and the body of the
lambda consists only of a single return statement.
*/

#include <iostream>

int main() {
    // Define a constexpr lambda that takes two ints and returns their sum
    constexpr auto add = [](int a, int b) constexpr -> int { return a + b; };

    // Use the lambda in a constexpr context
    constexpr int result = add(3, 4);
    std::cout << result << std::endl; // Outputs: 7

    // Use the lambda in a non-constexpr context
    int x = 5, y = 6;
    int sum = add(x, y);
    std::cout << sum << std::endl; // Outputs: 11

    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: