Intuition The one core idea
A callable is anything you can invoke with parentheses, like thing(2,3) — a plain function, a lambda, a functor, or a member function. std::function is a single box that hides what kind of callable is inside and only remembers the shape of the call , so you can store and swap any of them freely.
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.
To call (or invoke ) a function is to write its name followed by parentheses holding its inputs: add(2, 3). The machine jumps to the code, runs it with those inputs, and hands you back a result.
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.
Normally the box has a fixed name (add) glued to a fixed spot in your program. The parent topic wants to unglue it: store the box in a variable, put it in a list, hand it to someone else, swap it out later. To do that we first need words for "the box" and "the shape of its parentheses".
A function's signature is its return type plus the types of its inputs , written R(Args...).
R = the type of the value that comes out .
Args... = the list of types that must go in .
Concretely, add takes two ints and returns one int, so its signature is int(int, int).
Intuition Picture the signature as a socket shape
Think of a wall socket. The signature is the shape of the holes . Any plug (callable) with matching prongs fits. int(int,int) = "two integer prongs in, one integer prong out". This shape is the only thing std::function insists on.
Common mistake Signature is not the whole function
Feels right: "the signature identifies the function." Reality: many different functions share the signature int(int,int) — add, subtract, a lambda, a functor. The signature is deliberately vague; that vagueness is exactly what lets one std::function type hold all of them.
A type is the label C++ puts on every value and every variable that says what it is and what you can do with it : int, double, std::string, and — importantly — even functions have types .
Intuition Why type matters here
C++ demands that everything in a single variable, or a single list, share one type . That single rule is the whole reason std::function has to exist: the four kinds of callable each have different types, so they can't share a variable — until we wrap them.
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.
Definition Function pointer
A function pointer is a variable that stores the address of a function — literally where in memory the code lives — so you can call the function through the variable instead of by name.
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.
Intuition Why the topic mentions this
Function pointers are the oldest way to store-and-pass a callable, and they only work for plain free functions. std::function is the modern, universal upgrade — but you must know what it replaces to appreciate it.
(*) are not multiplication
Inside a type, * means "pointer to". int(*)(int,int) has nothing to do with multiplying — it's one indivisible spelling for "pointer to this kind of function".
Definition Lambda expression
A lambda is a little function you write right where you need it , with no name. Its shape is [capture](params){ body }.
[...] — the capture list : outside variables the lambda remembers.
(...) — the parameters, just like a normal function.
{...} — the body.
Worked example The same "subtract" as a lambda
auto sub = []( int a , int b ){ return a - b; };
sub ( 5 , 2 ); // 3
No name was declared; sub is just a variable holding it. Full details in Lambda Expressions .
Intuition The crucial, weird fact
Every lambda has its own unique, unnameable type — even two lambdas that look identical are different types to the compiler. That is why you cannot make a vector of lambdas directly, and why std::function (which hides the type) is the hero of the parent note.
A functor is an ordinary object of a class that defines a special method named operator(). Because that method exists, you may write obj(args) — the object behaves like a function.
Worked example Multiply as a functor
struct Mul {
int operator () ( int a , int b ) const { return a * b; }
};
Mul m;
m ( 2 , 3 ); // 6 — looks like a function call, is actually m.operator()(2,3)
See Functors and operator() .
Intuition Why functors exist
Unlike a plain function, a functor is an object , so it can carry state (member variables) between calls. A lambda with a capture list is secretly compiled into a functor — so understanding functors demystifies lambdas.
Definition Member function
A member function is a function that lives inside a class and operates on a particular object. To call c.addBase(5) you need two things: the code, and the object c it runs on.
Intuition The secret first parameter
Every member function secretly receives the object as a hidden first argument called this. The figure shows addBase(x) really being addBase(this, x). This is exactly why the parent's std::bind(&Counter::addBase, &c, _1) passes the object &c as the first bound argument — you are filling in that hidden this.
&Counter::addBase
The & means "take the address of", and Counter::addBase names a member of the Counter class. Together &Counter::addBase is a pointer-to-member-function — the address of the member's code, not yet attached to any object.
A template is a blueprint that generates code for whatever type you plug in . std::function<R(Args...)> is a template: you plug in a signature, it manufactures the matching box type. See Templates and Type Erasure .
Type erasure is the trick of hiding the concrete type behind a uniform interface. std::function stores "some callable" but only exposes "you can call me with Args... and get an R". The original type is erased from view.
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.
Definition Angle brackets
<>
<...> supply a template its type arguments. std::function<int(int,int)> reads as "a function-box configured for the signature int(int,int)".
... (parameter pack)
Args... means "zero or more parameter types". It lets one template cover void(), int(int), int(int,int), and every other shape.
_1, _2, ...
Living in std::placeholders, these are markers for arguments supplied later . _1 = "the 1st argument the caller gives when calling the finished (bound) object", _2 = the 2nd, and so on — not positions in the bind list.
_1 ≠ "first thing I bound"
Feels right: "1 = first in the list." Reality: _n counts arguments of the future call. Read it aloud as "slot for the n-th argument the caller passes later."
&
A reference is an alias : another name for an existing object, so changing one changes the other. No copy is made.
Intuition Why the topic needs
std::ref
std::bind copies every argument you give it. If you actually wanted to share the same object (not a copy), you wrap it: std::ref(obj) says "store a reference, not a copy", and std::cref(obj) does the same but read-only. See std::ref and std::cref .
Member functions hidden this
std bind and placeholders
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.
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.