std::bind
Bind arguments to callables.
std::bind is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is std::bind?
std::bind creates a new callable by fixing some arguments of an existing one. This is called partial application.
- Lives in
<functional>. - Returns an unspecified callable object.
#include <iostream>
#include <functional>
int add(int a, int b) { return a + b; }
int main() {
auto add10 = std::bind(add, 10, 5);
std::cout << add10() << '\n';
return 0;
}Placeholders
Placeholders like std::placeholders::_1 mark arguments to be supplied later when the bound object is called.
#include <iostream>
#include <functional>
int add(int a, int b) { return a + b; }
int main() {
using namespace std::placeholders;
auto add10 = std::bind(add, 10, _1);
std::cout << add10(7) << '\n';
return 0;
}Reordering Arguments
Placeholders can appear in any order, letting you swap the positions of the arguments the caller provides.
#include <iostream>
#include <functional>
int sub(int a, int b) { return a - b; }
int main() {
using namespace std::placeholders;
auto flipped = std::bind(sub, _2, _1);
std::cout << flipped(3, 10) << '\n';
return 0;
}Binding Several Arguments
You can fix multiple arguments at once while leaving others as placeholders.
#include <iostream>
#include <functional>
int volume(int l, int w, int h) { return l * w * h; }
int main() {
using namespace std::placeholders;
auto box = std::bind(volume, 2, _1, 5);
std::cout << box(3) << '\n';
return 0;
}Storing the Result
The object returned by std::bind can be stored in a std::function for a concrete, named type.
#include <iostream>
#include <functional>
int add(int a, int b) { return a + b; }
int main() {
using namespace std::placeholders;
std::function<int(int)> add100 = std::bind(add, 100, _1);
std::cout << add100(1) << '\n';
return 0;
}Binding Member Functions
std::bind can target a member function by passing the member pointer and an object (or placeholder) for this.
#include <iostream>
#include <functional>
struct Counter {
int add(int x) { return value += x; }
int value = 0;
};
int main() {
using namespace std::placeholders;
Counter c;
auto bump = std::bind(&Counter::add, &c, _1);
std::cout << bump(5) << ' ' << bump(3) << '\n';
return 0;
}Binding by Value vs Reference
Bound arguments are copied by default. Wrap one in std::ref to bind by reference so changes are visible.
#include <iostream>
#include <functional>
void addTo(int& target, int amount) { target += amount; }
int main() {
using namespace std::placeholders;
int total = 0;
auto acc = std::bind(addTo, std::ref(total), _1);
acc(5);
acc(10);
std::cout << total << '\n';
return 0;
}Using Bound Callables in Algorithms
A bound callable works as a predicate or operation in standard algorithms.
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
bool greaterThan(int x, int limit) { return x > limit; }
int main() {
using namespace std::placeholders;
std::vector<int> v = {1, 5, 8, 2, 9};
auto over4 = std::bind(greaterThan, _1, 4);
std::cout << std::count_if(v.begin(), v.end(), over4) << '\n';
return 0;
}Nesting bind
Bind expressions can be composed, though this quickly becomes hard to read. Each placeholder still refers to the final caller's arguments.
#include <iostream>
#include <functional>
int add(int a, int b) { return a + b; }
int times(int a, int b) { return a * b; }
int main() {
using namespace std::placeholders;
auto f = std::bind(add, std::bind(times, 2, _1), 3);
std::cout << f(5) << '\n';
return 0;
}Ignoring Extra Arguments
If the bound call uses only some placeholders, any extra arguments passed at call time are simply ignored.
#include <iostream>
#include <functional>
int show(int a) { return a; }
int main() {
using namespace std::placeholders;
auto f = std::bind(show, _1);
std::cout << f(42, 99, 100) << '\n';
return 0;
}When a Lambda Is Clearer
Modern C++ often prefers a lambda over std::bind because it is more readable and the compiler optimizes it better.
#include <iostream>
#include <functional>
int add(int a, int b) { return a + b; }
int main() {
auto add10 = [](int x) { return add(10, x); };
std::cout << add10(5) << '\n';
return 0;
}Quick Check
Test your understanding of placeholders.
Recap
You learned how std::bind works:
- fixes some arguments and uses placeholders
_1,_2for the rest - can reorder arguments, bind member functions, and use
std::ref - results can be stored in
std::functionand used in algorithms - lambdas are often clearer in modern C++
Next, you will compare lambdas, bind, and function pointers to pick the right tool.
Frequently asked questions
Is the “std::bind” lesson free?
Yes — the full text of “std::bind” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “std::bind”?
Bind arguments to callables. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “std::bind” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.