std::binder...d::binder2nd(3)
π NAME
std::binder1st, std::binder2nd β ok so these are the original argument-binding function objects, bestie. They literally take a binary function and glue one argument into place so it becomes unary. Historic. Iconic. Deprecated. Gone. π
β¨ Synopsis
Defined in header <functional> β that's where they live, no cap.
template< class Fn >
class binder1st
: public std::unary_function<typename
Fn::second_argument_type,
typename Fn::result_type>
{
protected:
Fn op;
typename Fn::first_argument_type value;
public:
binder1st( const Fn& fn, (1)
(deprecated in C++11)
const typename Fn::first_argument_type&
(removed in C++17)
value );
typename Fn::result_type
operator()(const typename
Fn::second_argument_type& x) const;
typename Fn::result_type
operator()(typename Fn::second_argument_type& x)
const;
};
template< class Fn >
class binder2nd
: public std::unary_function<typename
Fn::first_argument_type,
typename Fn::result_type>
{
protected:
Fn op;
typename Fn::second_argument_type value;
public:
binder2nd( const Fn& fn, (2)
(deprecated in C++11)
const typename Fn::second_argument_type&
(removed in C++17)
value );
typename Fn::result_type
operator()(const typename Fn::first_argument_type&
x) const;
typename Fn::result_type
operator()(typename Fn::first_argument_type& x)
const;
};
π¦ so what IS this thing
It's a function object that binds an argument to a binary function. That's the whole vibe. π
The value of the parameter gets passed to the object at construction time and then stored inside the object, like it's keeping a little secret. Whenever the function object gets invoked through operator(), that stored value gets passed as one of the arguments, and the other argument comes in as the argument of operator(). End result? A unary function. One in, one out. main character energy. β¨
- Binds the first parameter to the value
valuegiven at the construction of the object. - Binds the second parameter to the value
valuegiven at the construction of the object.
β οΈ Heads up bestie: these are deprecated in C++11 and removed in C++17. If you're writing new code, this is a museum piece, not a tool. ποΈ
πΈ Example
// Run this code
#include <cmath>
#include <functional>
#include <iostream>
#include <vector>
const double pi = std::acos(-1); // use std::numbers::pi in C++20
int main()
{
// deprecated in C++11, removed in C++17
auto f1 = std::bind1st(std::multiplies<double>(), pi / 180.0);
// C++11 replacement
auto f2 = [](double a) { return a * pi / 180.0; };
for (double n : {0, 30, 45, 60, 90, 180})
std::cout << n << "Β°\t" << std::fixed << "= "
<< f1(n) << " rad (using binder)\t= "
<< f2(n) << " rad (using lambda)\n"
<< std::defaultfloat;
}
π Output
0Β° = 0.000000 rad (using binder) = 0.000000 rad (using lambda)
30Β° = 0.523599 rad (using binder) = 0.523599 rad (using lambda)
45Β° = 0.785398 rad (using binder) = 0.785398 rad (using lambda)
60Β° = 1.047198 rad (using binder) = 1.047198 rad (using lambda)
90Β° = 1.570796 rad (using binder) = 1.570796 rad (using lambda)
180Β° = 3.141593 rad (using binder) = 3.141593 rad (using lambda)
Look at that β the binder and the lambda agreeing on everything. Bestie behaviour fr fr. π«Ά
π§ Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. (Yes, they went back and fixed history. slay.)
LWG 109β applied to C++98. Behavior as published:operator()could not modify the argument passed to it. Correct behavior: added overloads to handle this. π
π See also
bind1stβ binds one argument to a binary function (function template) (deprecated in C++11)(removed in C++17)bind2ndβ binds one argument to a binary function (function template) (deprecated in C++11)(removed in C++17)
and that's the whole page, diva β¨ this one came from http://cppreference.com, dated 2024.06.10 β go drink some water and don't use deprecated binders ππ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms