Back to questions
JuniorTheoryCommon
What does the program print?
#include <iostream>
int main() {
int x = 1;
auto get = [x]() { return x; };
x = 5;
std::cout << get() << ' ' << x;
}1 5
- Expecting
5 5:[x]copiesxwhen the lambda is created, so later changes toxdo not reach it. - Expecting
1 1:[x]copiesxinto the lambda, butxitself stays an ordinary variable, sox = 5changes it.
- What does the program print if the capture is
[&x]instead of[x]? - Why does
[x]() { ++x; }not compile withoutmutable?