What is a lambda expression?
It was introduced in C++11.
Lambda Expressions · Exercise 1 of 4
Twenty multiple-choice questions on C++ lambda expressions: syntax, captures, mutable, init captures, generic lambdas, closure types, std::function and lifetimes. Pick an option to check your answer instantly.
What is a lambda expression?
It was introduced in C++11.
In [limit](int x) -> bool { return x < limit; }, what is [limit]?
(int x) is the parameter list and -> bool the return type.
When is the variable x copied in auto f = [x] { return x; };?
Later changes to x are not seen by f.
What does the capture [&] do?
[=] does the same by value.
Why must you write mutable to modify a by-value capture?
mutable changes the lambda's own copy, never the original variable.
Do auto a = []{ return 1; }; and auto b = []{ return 1; }; have the same type?
That is why you store lambdas in auto, templates or std::function.
Which lambda can be assigned to int (*fp)(int)?
Only lambdas without captures convert; captured state has nowhere to live in a plain function pointer.
What is [](auto a, auto b) { return a + b; }?
It works with ints, doubles, strings – anything supporting +.
What does [p = std::move(ptr)] allow?
Init captures (C++14) can initialise a member from any expression.
What is the difference between capturing [this] and [*this]?
[*this] is safer for asynchronous callbacks that may run after the object is gone.
Timed online tests with instant scores and explanations.