Interview Q&A · C++ · 7 questions in this topic

Lambdas, auto & Callables — C++ Interview Q&A

All C++ topics

Modern callable plumbing — lambda captures, `auto`, `std::function` vs templates, and how lambdas actually desugar. Part of Functions & Lambdas · 213 questions in the C++ bank.

What is a lambda expression and what do the capture clauses mean?

Answer

A lambda is an anonymous function object: [captures](params) specifiers -> returnType { body }. Captures control what the lambda closes over: [] nothing, [=] copy everything used, [&] reference everything used, [x] copy x, [&x] reference x, [this] the object pointer, [*this] a copy of the object (C++17).

Default captures are convenient but risky — [&] can dangle when the lambda outlives the scope, which is why explicit captures are preferred in stored callbacks.

Example

Example.cppcpp
int factor = 3;
auto by_copy = [factor](int x) { return x * factor; };
auto by_ref = [&factor](int x) { factor += x; return factor; };
std::vector<int> v{1,2,3};
int sum = 0;
for (int x : v) sum += by_copy(x);

What does a lambda desugar to?

Answer

The compiler generates an unnamed functor class (closure type) with captured values as members and operator() as the body, plus a constructor capturing them. Each lambda expression has a unique, unnamed type — which is why you store lambdas in auto, not by naming their type.

A captureless lambda additionally provides a conversion to a plain function pointer, since it needs no state.

Example

Example.cppcpp
// conceptually, [factor](int x){ return x*factor; } becomes:
struct __Lambda {
    int factor;
    int operator()(int x) const { return x * factor; }
};

💡 Saying each lambda has a unique unnamed type explains in one stroke why auto is the natural way to hold one.

What does `auto` do, and when should you (and should you not) use it?

Answer

auto deduces the variable's type from its initializer at compile time — no runtime cost, and often more correct than spelling the type (it cannot accidentally narrow or drop references/const the way a hand-written type can). Use it for iterators, complex template types, lambdas and range-for over containers.

Do not use it where the deduced type surprises: auto x = {1,2,3} deduces initializer_list, auto drops top-level const/ref unless you write auto& / const auto&, and auto return types need care with dangling references.

Example

Example.cppcpp
std::map<std::string, std::vector<int>> m;
auto it = m.find("k"); // no one wants to spell this type
const auto& ref = m.at("k"); // keep const + reference!
auto&& any = get_value(); // forwarding reference binds to anything

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits