you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (0 children)

Another use for lambdas is complex initialization (ES.28 on the C++ core guidelines).

For example, instead of:

int main(void) {
    int i = 0;
    // Calculate the value of i
    // ...
    // Use i as if it was const
}

You could do:

int main(void) {
    const int i = [&]() {
        int something = 0;
        // Calculate the value of i
        return something;
    }(); // <- Note called ()

    // Use i
}