All challenges
Code AnalysisBeginner 60s

Lambda capture by reference outliving its scope

What is the bug in this code?
Starter code
#include <functional>
#include <iostream>

std::function<int()> make() {
    int x = 42;
    return [&x]() { return x; };
}

int main() {
    auto f = make();
    std::cout << f() << '\n';
}

Think you can answer under pressure?

Get matched and answer live questions in 3-minute 1v1 battles. Elo ranking, badge tiers and bragging rights on the line.

Play this in a 3-minute battle