5.2.23 · D1C++ Programming

Foundations — std - function and std - bind

2,003 words9 min readBack to topic

Before you can understand std::function and std::bind, you must be fluent in the vocabulary the parent note assumes. This page builds every one of those pieces from nothing — a smart 12-year-old who has never touched C++ should finish able to read the parent note line by line.


0. What does "call a function" even mean?

Figure — std - function and std - bind

Look at the figure: the arguments 2 and 3 go in through the parentheses, the function body does its work, and a return value 5 comes out. Everything on this whole page is about capturing that little machine — the box — and moving it around.


1. Return type and parameter types — the shape of a call

Concretely, add takes two ints and returns one int, so its signature is int(int, int).


2. Type — why C++ is so fussy about it

The type of the free function add is written int(*)(int,int) — read as "pointer to a function taking two ints, returning int". That leads directly to the next symbol.


3. The function pointer int(*)(int,int)

Figure — std - function and std - bind

In the figure the yellow box p doesn't contain the adding machine; it holds an arrow to it. Writing p(2,3) follows the arrow and runs add. See Function Pointers for the full treatment.


4. Lambda — an anonymous function you write inline


5. Functor — an object you can call like a function


6. Member function and the hidden this

Figure — std - function and std - bind

7. Templates and type erasure — the trick behind the box

Figure — std - function and std - bind

The figure shows three plugs of different shapes (function pointer, lambda, functor) all fitting one adapter — the adapter is std::function. Downstream code sees only the adapter's socket shape (the signature), never the plug behind it.


8. Angle brackets, ..., and the underscore placeholders


9. References, std::ref, and std::cref


The prerequisite map

Calling a function

Signature R of Args

Types in C plus plus

Function pointers

Lambdas unique type

Functors operator paren

Member functions hidden this

Templates

Type erasure

std function

std bind and placeholders

References and std ref

std function and std bind

Every arrow says "you need the left idea to make sense of the right one". Notice both std::function and std::bind sit just below the parent topic the parent topic — they are the last two bricks.


Equipment checklist

Test yourself — cover the right side and answer aloud.

What does it mean to call a function?
Write its name and parentheses with inputs; jump to its code, run it, get a return value back.
What is a function's signature?
Its return type plus its parameter types, written R(Args...).
Why can't two different lambdas share one variable?
Each lambda has its own unique unnamed type, and C++ needs one type per variable.
What is a function pointer?
A variable holding the memory address of a function, so you can call the function through it.
What makes an object a functor?
It defines operator(), so you can invoke it like obj(args).
What hidden argument does every member function receive?
The object it runs on, called this, passed as a secret first parameter.
What is type erasure?
Hiding a value's concrete type behind a uniform interface — here, "I can be called with Args... returning R".
What does the placeholder _2 mean?
The second argument the caller supplies when the bound object is later called.
Why do you need std::ref?
Because std::bind copies arguments by value; std::ref(x) stores a reference instead so the same object is shared.
What is the whole topic's one-line idea?
std::function is a uniform box that stores any callable of a given signature; std::bind pre-fills some of its arguments.

Ready? Then head back to std::function and std::bind and every symbol will already be familiar.