Why do C++ codebases now prefer lambdas or std::bind_front over std::bind, and what does std::bind do that surprises people?
answer
- bound arguments are stored by copy
- std::ref to bind a reference
- extra call arguments vanish
- overload sets do not deduce
basics
~20 sstd::bind stores copies of its bound arguments, silently drops extra call arguments and cannot take an overloaded function name, so it hides bugs. A lambda states the call explicitly, and C++20 std::bind_front covers partial application without placeholders.
solid answer
~40 s`std::bind(f, args...)` returns a call wrapper that stores **decayed copies** of the bound arguments, so `std::bind(record, stats, _1)` updates a copy of `stats` unless you write `std::ref(stats)`. Arguments passed at the call that no placeholder refers to are **accepted and discarded**, so `b(3, "x", 99)` compiles. An **overloaded function name** cannot be bound without a cast, because `bind` must deduce one function type. Bound arguments that are themselves bind expressions are called first and their results passed, which surprises readers of nested binds. A lambda avoids all of this: the parameters, captures and call are written out. For simple partial application, **C++20** added `std::bind_front` (and C++23 `std::bind_back`), which forward the remaining call arguments in order; they still copy what they bind, so references need `std::ref` there too.
code
cpp · 21 lines#include <cstdio>
#include <functional>
struct Stats { int submitted = 0; };
void record(Stats& s, int job_id) { ++s.submitted; std::printf("job %d\n", job_id); }
int main() {
using namespace std::placeholders;
Stats stats;
auto by_copy = std::bind(record, stats, _1); // stores a COPY of stats
auto by_ref = std::bind(record, std::ref(stats), _1); // stores a reference_wrapper
by_copy(1);
by_ref(2);
by_ref(3, "ignored", 99); // extra arguments are silently discarded
auto lambda = [&stats](int id) { record(stats, id); }; // the usual replacement
lambda(4);
auto front = std::bind_front(record, std::ref(stats)); // C++20
front(5);
std::printf("submitted=%d\n", stats.submitted);
}go deeper
Know that std::bind fixes some arguments of a call and that a lambda is the usual modern replacement.
Explain the decayed copies and std::ref, the discarded extra arguments, the overload problem, and what std::bind_front and std::bind_back offer.
Spot the bug class in review: state updated on a hidden copy, or a silently ignored argument, and rewrite the binder as an explicit lambda.
Set a codebase rule on binders, typically lambdas everywhere with bind_front tolerated for plain partial application, and plan the migration of legacy bind calls.
## What std::bind builds `std::bind`, in `<functional>` since C++11, takes a callable and a list of **bound arguments** and returns an unnamed **call wrapper**. Each bound argument is either a value, stored in the wrapper, or a **placeholder** such as `std::placeholders::_1`, meaning "the first argument of the eventual call". It was the standard tool for partial application before lambdas were common; today most style guides prefer a lambda. The reasons are concrete, and each is visible in the code example. ## The surprises 1. **Bound arguments are copied.** The standard stores each bound argument as `decay_t<T>`, initialized from what you passed. `std::bind(record, stats, _1)` therefore holds its own `Stats`, and calls through it update that copy. In the example, five jobs were recorded but `submitted=4`, because the first went to the copy. Binding a reference needs `std::ref(stats)` (or `std::cref`), which stores a `std::reference_wrapper` that the wrapper unwraps at the call. 2. **Extra call arguments are discarded.** The wrapper accepts any number of call arguments and forwards only those named by placeholders. `by_ref(3, "ignored", 99)` compiles and runs; a typo that passes the wrong argument count goes unnoticed. 3. **Overloaded names do not bind.** `std::bind(log_job, _1)` with two `log_job` overloads fails; Clang reports `no matching function for call to 'bind'`, because `bind` must deduce the type of `F` from an overload set. You need a cast to the exact function pointer type, or a lambda that lets ordinary overload resolution run at the call. 4. **Nested bind expressions are evaluated first.** If a bound argument is itself the result of `std::bind`, the outer wrapper calls it with the same call arguments and passes its result, instead of passing the wrapper object. That is occasionally useful and often a surprise. 5. **The result type is unspecified.** You hold it with `auto`, a template parameter or `std::function`; you cannot name it. ## The replacements | Tool | Since | What it does | Binds references with | |---|---|---|---| | lambda | C++11 | spells out parameters, captures and the call | `[&stats]` | | `std::bind_front(f, a)` | C++20 | `f(a, call_args...)` | `std::ref(a)` | | `std::bind_back(f, a)` | C++23 | `f(call_args..., a)` | `std::ref(a)` | | `std::bind(f, a, _1)` | C++11 | placeholders in any order | `std::ref(a)` | A lambda is the most explicit: `[&stats](int id) { record(stats, id); }` shows that `stats` is referenced, takes exactly one `int`, and lets overload resolution pick `record` normally. `std::bind_front` is shorter for the common "fix the first arguments" case and has no placeholders, but its bound arguments are decayed copies just like `bind`'s: the example passes `std::ref(stats)` for that reason. ## Rewriting a binder as a lambda 1. Read the bound argument list left to right: values become captures, placeholders become parameters. 2. Decide each capture's mode on purpose: a value that `bind` copied becomes a by-copy capture, a `std::ref` becomes a by-reference capture. 3. Give the lambda exactly as many parameters as the placeholders used, so that a wrong argument count becomes a compile error. For example, `std::bind(cleanup, _2, _1)` swaps its two arguments; the lambda `[](int a, int b) { cleanup(b, a); }` does the same and says so. Both printed `cleanup 2 1` when called with `(1, 2)`. ## When std::bind still appears - Older code written before lambdas could capture by move or be generic. - Reordering or duplicating arguments with placeholders, which `bind_front` and `bind_back` cannot do; a lambda can, and reads better. - Code that relies on nested bind evaluation, which is better rewritten as a lambda that makes the inner call explicit. ## Mistakes interviewers listen for - Assuming `std::bind` binds by reference because the target takes `Stats&`. - Assuming the wrapper checks the number of call arguments. - Assuming `std::bind_front` binds by reference where `bind` copies. - Reaching for `bind` to pass a member function to an algorithm, where a lambda or `std::invoke`-aware code is clearer. ## How to answer List the copy semantics and `std::ref`, the silently discarded arguments and the overload problem, then say that lambdas make the call explicit and that `std::bind_front` and `std::bind_back` cover simple partial application from C++20 and C++23.
- How do you bind a member function to an object with std::bind_front?Pass the member pointer first and the object second: `std::bind_front(&Cache::flush, &cache)` stores a copy of the pointer, and each call runs `cache.flush(call_args...)` through INVOKE. Passing `cache` by value would copy the whole object into the wrapper; `std::ref(cache)` or a pointer keeps the original. A lambda, `[&cache] { cache.flush(); }`, says the same thing explicitly.
- Is a lambda slower than std::bind?Not as a rule. A lambda is a function object whose call operator is visible to the compiler, so the call is easily inlined. A bind wrapper is also a function object, and optimizers usually see through it, but its template machinery adds compile time and makes the generated code harder to read. Measure before claiming either is faster.
- std::bind stores a reference when the target function takes a reference parameter.
- Passing too many arguments to a bind object is a compile error.
- std::bind_front binds its arguments by reference, unlike std::bind.
- std::bind can bind an overloaded function name without a cast.
- Placeholders such as _1 are in the std namespace directly.
Assumes C++23: std::bind dates from C++11, std::bind_front from C++20 and std::bind_back from C++23.