3 C++ Lambdas Myths


3 C++ Lambdas Myths

Lambdas have been a powerful feature of C++ since their introduction in C++11. They are used a lot, but there are several myths about what lambdas are and how they behave. Let’s clear up some of the most common misconceptions.


Myth 1: A Lambda Is Just a Function

One of the most common explanations you'll hear is that a lambda is "a nameless function you can write inline." It's true that lambdas are callable like functions, but they are not actually functions. Instead they are function objects.

Lambdas can capture and maintain internal state, something regular functions can’t do on their own. Here's an example:

Where is the count variable stored? It lives inside the counter object. That’s possible because lambdas are actually instances of a compiler-generated class with an overloaded operator(), expressed with lambda syntax.

You could rewrite the lambda above like this:

Want to dive deeper into lambdas? Check out my new eBook The Anatomy of C++ Lambdas


Myth 2: Lambdas and std::function Are Interchangeable

Many developers confuse lambdas with std::function, but they are not the same. As we've seen, a lambda is simply a function object.

On the other hand, std::function is a type-erased wrapper for anything callable: regular functions, function pointers, or function objects (like lambdas). This abstraction comes at a cost.

std::function may incur a performance and memory cost because it typically involves dynamic allocation on the heap to store the callable. A common pattern for std::function is to inject behaviour into objects, see example below from new eBook The Anatomy of C++ Lambdas


Myth 3: Lambdas Are Slow

This myth often stems from confusing lambdas with std::function. As we’ve seen, lambdas are just syntactic sugar around function objects, and in most cases, they are inlined and optimized aggressively by the compiler.

Unless you’re capturing large state or passing them through std::function, lambdas have no overhead.

Still skeptical? Benchmark your code or inspect the compiler output using a tool like Compile Explorer. Compile with `-O3` and see for yourself how efficiently lambdas are handled. 


Lambdas are one of the most misunderstood features in modern C++. If you'd like to explore more about how they work under the hood, check out my new eBook The Anatomy of C++ Lambdas.