Caffeinated Coder
Book an appointment
Lesson 22 · Modern C++

Lambdas: functions made on the spot

A lambda expression writes a small function right where you need it, for example as the test you hand to std::count_if. The compiler turns it into an object of a hidden class: captured variables become its data, and the body becomes its call operator. Follow one lambda as it copies limit, keeps its 5 when limit changes to 100, and gets called once per element, then see how [&limit] changes the answer and when a reference can dangle.

11 min10 steps5 questionsAdvanced
Fig 22 · Interactive
Loading figure…
or ← →

Quiz Unlocks after the last step.