std::bind1st,std::bind2nd(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– NAME πŸ’–

std::bind1st, std::bind2nd β€” bestie, this is the std::bind1st,std::bind2nd(3) page from the C++ Standard Libary (yes, that spelling is literally in the header, don't come for me 😭). It’s the old-school pair for binding one argument to a binary function object.

✨ Synopsis ✨

Okay so first thing: it’s defined in header <functional> 🫢

Defined in header <functional>

template< class F, class T >                         (1) (deprecated in C++11)
std::binder1st<F> bind1st( const F& f, const T& x );     (removed in C++17)

template< class F, class T >                         (2) (deprecated in C++11)
std::binder2nd<F> bind2nd( const F& f, const T& x );     (removed in C++17)

So the vibe is: it binds a given argument x to a first or second parameter of the given binary function object f. That is, stores x within the resulting wrapper, which, if called, passes x as the first or the second parameter of f. Basically it’s holding your little argument for you and handing it over at the right spot. Periodt πŸ’…

  1. Binds the first argument of f to x. Effectively calls std::binder1st<F>(f, typename F::first_argument_type(x)).
  2. Binds the second argument of f to x. Effectively calls std::binder2nd<F>(f, typename F::second_argument_type(x)).

πŸŽ€ Parameters πŸŽ€

  • f β€” pointer to a function to bind an argument to
  • x β€” argument to bind to f

🌸 Return value 🌸

A function object wrapping f and x. Simple as that, icon ✨

πŸ’… Exceptions πŸ’…

May throw implementation-defined exceptions. No cap.

πŸ¦‹ Example πŸ¦‹

The original has a little // Run this code marker at the top, so I’m keeping it β€” here’s the whole thing, bestie:

// Run this code

#include <algorithm>
#include <cmath>
#include <cstddef>
#include <functional>
#include <iomanip>
#include <iostream>
#include <vector>

int main()
{
    std::vector<double> a = {0, 30, 45, 60, 90, 180};
    std::vector<double> r(a.size());
    const double pi = std::acos(-1); // since C++20 use std::numbers::pi

    std::transform(a.begin(), a.end(), r.begin(),
        std::bind1st(std::multiplies<double>(), pi / 180.0));
//  an equivalent lambda is: [pi](double a) { return a * pi / 180.0; });

    for (std::size_t n = 0; n < a.size(); ++n)
        std::cout << std::setw(3) << a[n] << "Β° = " << std::fixed << r[n]
                  << " rad\n" << std::defaultfloat;
}

⭐ Output ⭐

This is giving exactly:

  0Β° = 0.000000 rad
 30Β° = 0.523599 rad
 45Β° = 0.785398 rad
 60Β° = 1.047198 rad
 90Β° = 1.570796 rad
180Β° = 3.141593 rad

🌈 See also 🌈

  • binder1st, binder2nd β€” function object holding a binary function and one of its arguments (class template) (deprecated in C++11)(removed in C++17)
  • bind_front β€” bind a variable number of arguments, in order, to a function object (function template) (C++20)
  • bind_back β€” bind a variable number of arguments, in order, to a function object (function template) (C++23)

source: http://cppreference.com Β· version/date: 2024.06.10 β€” mwah, bestie πŸ’‹πŸ’–

πŸ“¦ where this page comes from