Advertisement
Intermediate C++, Lambda Expressions

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

Short 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.

Save C++ mock interview
Advertisement
AModel 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.
Likely follow-ups
  • How does an IILE help with const? | What are the parentheses at the end for?
Related topics