Practice mode C++

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.