Namespaces
Variants

std:: binder1st, std:: binder2nd

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
binder1st binder2nd
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
Definiert im Header <functional>
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,
const typename Fn :: first_argument_type & 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 ;

} ;
(1) (veraltet in C++11)
(entfernt in C++17)
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,
const typename Fn :: second_argument_type & 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 ;

} ;
(2) (veraltet in C++11)
(entfernt in C++17)

Ein Funktionsobjekt, das ein Argument an eine binäre Funktion bindet.

Der Wert des Parameters wird zum Zeitpunkt der Konstruktion an das Objekt übergeben und innerhalb des Objekts gespeichert. Immer wenn das Funktionsobjekt über operator() aufgerufen wird, wird der gespeicherte Wert als eines der Argumente übergeben, das andere Argument wird als Argument von operator() übergeben. Das resultierende Funktionsobjekt ist eine unäre Funktion.

1) Bindet den ersten Parameter an den Wert value , der bei der Konstruktion des Objekts angegeben wurde.
2) Bindet den zweiten Parameter an den Wert value , der bei der Konstruktion des Objekts angegeben wurde.

Beispiel

#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;
}

Ausgabe:

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)

Fehlerberichte

Die folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.

DR Angewendet auf Verhalten wie veröffentlicht Korrektes Verhalten
LWG 109 C++98 operator() konnte das übergebene Argument nicht modifizieren Überladungen zur Behandlung hinzugefügt

Siehe auch

(in C++11 veraltet) (in C++17 entfernt)
bindet ein Argument an eine binäre Funktion
(Funktions-Template)