1. Problem It Solves
C++ callables have different concrete types; std::function erases those types, std::bind pre-binds arguments, and std::ref preserves reference semantics. It makes an important constraint visible instead of leaving readers to guess. This lesson keeps only the C++11 core that fits one focused day.
2. Prerequisites
The ideas from Day 36, plus basic variables, functions, and output already introduced.
3. Core Idea
Mental model: C++ callables have different concrete types; std::function erases those types, std::bind pre-binds arguments, and std::ref preserves reference semantics. Identify the relevant value or state, who owns it, and whether the rule acts during compilation or execution.
4. Minimal Syntax
int (*fp)(int,int)=add; std::function<int(int)> f = functor;5. How It Works
The example creates a tiny fixed state with no keyboard input.
C++11 or the standard-library contract applies today's rule.
The program prints the important result so it can be checked against the source.
6. Common Mistakes
std::bindcopies bound arguments by default, so forgettingstd::refupdates a private copy instead of the original object.
7. When to Use It
Use it when an interface needs to store or pass several compatible callable forms.
Avoid it when it hides ownership, lifetime, type, ordering, or cost.
8. Simple Example
The program calls a normal function through a pointer, a functor through std::function, and a bound increment that updates an integer via std::ref. The .cpp keeps the data fixed and avoids unrelated abstraction.
Complete sample code
Source file
cpp11/37_function_pointer_functor_std_function_bind_ref/main.cpp
#include <functional>
#include <iostream>
int add(int left, int right) {
return left + right;
}
struct Multiplier {
int operator()(int value) const {
return value * 3;
}
};
void increment(int& value, int amount) {
value += amount;
}
int main() {
int (*function_pointer)(int, int) = add;
std::function<int(int)> function_object = Multiplier();
int total = 10;
std::function<void(int)> bound =
std::bind(increment, std::ref(total), std::placeholders::_1);
std::cout << "pointer=" << function_pointer(2, 5) << '\n';
std::cout << "functor=" << function_object(4) << '\n';
bound(7);
std::cout << "total=" << total << '\n';
}
9. Key Takeaways
The feature is part of the C++11 scope used in this course.
Understand its lifetime, ownership, type, and ordering consequences.
Compile with warnings and prefer the smallest form that makes the rule obvious.
10. Self-Check Questions
Easy — How do
std::function,std::bind, andstd::refrespectively store a callable, bind arguments, and preserve a reference?Medium — Read the small example described above. What value or state should it print, and which rule produces that result?
Hard — Find and explain the subtle bug in this situation:
std::bindcopies bound arguments by default, so forgettingstd::refupdates a private copy instead of the original object. What is the smallest C++11-safe correction?