> In fact, it has existed long before lambdas where introduced.
Both std::function<> and lambdas were introduced in C++11.
Furthermore absolutely no one should use std::bind, it's an absolute abomination.
You are absolutely right of course! No idea, why I thought std::function existed before C++11. Mea culpa!
> Furthermore absolutely no one should use std::bind, it's an absolute abomination.
Agree 100%! I almost always use a wrapper lambda.
However, it's worth pointing out that C++20 gave us std::bind_front(), which is really useful if you want to just bind the first N arguments:
struct Foo { void bar(int a, int b, int c); }; Foo foo; using Callback = std::function<void(int, int, int)>; // with std::bind (ugh): using namespace std::placeholders; Callback cb1(std::bind(&Foo::bar, &foo, _1, _2, _3)); // lambda (without perfect forwarding): Callback cb2([&foo](auto&&... args) { foo.bar(args...); }); // lambda (with perfect forwarding): Callback cb3([&foo](auto&&... args) { foo.bar(std::forward<decltype(args)>(args)...); }); // std::bind_front Callback cb4(std::bind_front(&Foo::bar, &foo));
You are absolutely right of course! No idea, why I thought std::function existed before C++11. Mea culpa!
> Furthermore absolutely no one should use std::bind, it's an absolute abomination.
Agree 100%! I almost always use a wrapper lambda.
However, it's worth pointing out that C++20 gave us std::bind_front(), which is really useful if you want to just bind the first N arguments:
I think std::bind_front() is the clear winner here.