Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Memory and Utilities

C++ std::bind and std::bind_front

Reference for std::bind, std::bind_front and std::bind_back: fix arguments of a callable, use placeholders, and know when a lambda is the better choice.

std::bind, std::bind_front and std::bind_back (header <functional>) take a callable and some arguments and return a new callable object with those arguments already filled in. This is called partial application: a function of three parameters becomes a function of two once you fix one of them.

std::bind is the older tool (C++11) and supports reordering through placeholders such as std::placeholders::_1. std::bind_front (C++20) and std::bind_back (C++23) are simpler: they fix leading or trailing arguments only, and they are easier to read and to optimize. In new code a lambda is usually clearer than any of them.

C++
auto add10 = std::bind_front(std::plus<>{}, 10);
int r = add10(5);   // 15

Try it

C++ bind_front, bind and placeholders

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Bound arguments are copied

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
template<class F, class... Args>
/* unspecified */ bind(F&& f, Args&&... args);

template<class R, class F, class... Args>
/* unspecified */ bind(F&& f, Args&&... args);        // explicit return type R

template<class F, class... Args>
constexpr /* unspecified */ bind_front(F&& f, Args&&... args);   // C++20

template<class F, class... Args>
constexpr /* unspecified */ bind_back(F&& f, Args&&... args);    // C++23
Name Description
f The callable: function, function pointer, lambda, function object, or pointer to member.
args Values to store. With bind, _1, _2, ... are placeholders for the arguments of the later call.
R Optional explicit return type for bind, needed when the result type cannot be deduced.

Returns: an unspecified callable object. Its type is not nameable, so store it in auto or std::function.

Errors / undefined behavior: bound arguments are copied or moved into the result. A bound reference or a bound pointer that outlives its target is a dangling access when called later. Calling with too few arguments for the placeholders used is a compile error.

Usage notes

  • Copy by default. std::bind stores decayed copies of the arguments. Use std::ref and std::cref to bind by reference; otherwise a function taking T& modifies the stored copy.
  • Placeholders live in std::placeholders and refer to the position in the final call, not in the original function. Extra arguments in the final call are accepted and ignored by bind.
  • Nested binds are evaluated first: if an argument is itself a bind expression, it is called with the outer call arguments and its result is used.
  • Overloaded functions cannot be bound by name alone; cast to the right pointer type or use a lambda.
  • bind_front and bind_back forward the remaining arguments untouched, preserve value categories, and propagate noexcept-ness of the call. They do not support placeholders or reordering.
  • Prefer lambdas. [x](int y) { return f(x, y); } is easier to read, easier to debug, can be inlined, and has no surprises with overloads or copies. Use bind_front when you just fix a leading argument and want to say so.
  • Binding member functions: pass &Class::member and the object (or pointer, or std::ref(obj)) as the first bound argument. See std::invoke for the same calling rules.
  • std::bind_front and bind_back are not stored in a std::function any differently from other callables; the result is copyable if its parts are.