Behavior of std::bind and std::function

Let's take a step deeper into the workings of std::bind and std::function.

We'll cover the following

std::bind

Because of std::bind, you can create function objects in a variety of ways:

  • bind the arguments to an arbitrary position,
  • change the order of the arguments,
  • introduce placeholders for arguments,
  • partially evaluate functions,
  • invoke the newly created function objects, use them in the algorithm of the STL or store them in std::function.

std::function

std::function can store arbitrary callables in variables. It’s a kind of polymorphic function wrapper. A callable may be a lambda function, a function object, or a function. std::function is always necessary and can’t be replaced by auto, if you have to specify the type of the callable explicitly.