Back to results for “Lambda Expressions”
C++ › Lambda Expressions

What is an immediately invoked lambda expression (IILE) and why use it?

Intermediate

Answer

An IILE is a lambda that is called right where it's defined, using a trailing (). It's used to initialize a const variable with complex logic, to scope temporary variables, or to run one-off initialization while keeping the result const and the setup contained.
const int config = []{
    int v = load();
    return v * 2;
}();  // invoked immediately
Real-world example An IILE computes a complex const configuration value inline while keeping helper variables local.

Common follow-ups: How does an IILE help with const? | What are the parentheses at the end for?