C++ Quiz106 of 173
Difficulty:
⬅️ Prev: 228Quiz ID: 229Next: 230 ➡️

According to the C++23 standard, what is the output of this program?

#include <iostream>

int a = 1;

int main() {
    auto f = [](int b) { return a + b; };

    std::cout << f(4);
}
Hint

Only local variables may be captured.

Reveal answer

5

Explanation

Only local variables may be captured by a lambda. a is a global variable with static storage duration and may not be captured. §[expr.prim.lambda.capture]¶1:

The body of a lambda-expression may refer to local entities of enclosing block scopes by capturing those entities (...).

Since a is not captured, a in the lambda expression simply refers to the global variable a.

⬅️ Prev: 228Quiz ID: 229Next: 230 ➡️