Assuming standard headers and using namespace std, what does this C++ code print? int c = 0; auto f = () mutable { return ++c; }; f(); f(); cout << f() << c;
Correct answer A. 30
Explanation
A lambda's by-value captures are const by default; mutable allows the call operator to modify its own copy. That copy persists between calls, so successive calls return 1, 2 and 3. The outer variable c is never touched and stays 0. Without mutable, ++c inside the lambda would not compile.
Answer checked How we write and check questions
Discussion