名前空間
変種
操作

std::binder1st, std::binder2nd

From cppreference.com
< cpp‎ | utility‎ | functional
 
 
ユーティリティライブラリ
言語サポート
型のサポート (基本型、RTTI)
ライブラリ機能検査マクロ (C++20)
プログラムユーティリティ
可変引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20で非推奨)
 
関数オブジェクト
関数の呼び出し
(C++17)(C++23)
恒等関数オブジェクト
(C++20)
透過的な演算子ラッパー
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

古いバインダとアダプタ
(C++17まで*)
(C++17まで*)
(C++17まで*)
(C++17まで*)  
(C++17まで*)
(C++17*まで)(C++17*まで)(C++17*まで)(C++17*まで)
(C++20まで*)
(C++20まで*)
binder1stbinder2nd
(C++17*まで)(C++17*まで)
(C++17*まで)(C++17*まで)

(C++17まで*)
(C++17*まで)(C++17*まで)(C++17*まで)(C++17*まで)
(C++20まで*)
(C++20まで*)
 
ヘッダ <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) (C++11で非推奨)
(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) (C++11で非推奨)
(C++17で削除)

二項関数に引数をバインドする関数オブジェクト。

パラメータの値は、オブジェクト構築時にオブジェクトに渡され、オブジェクト内に格納されます。関数オブジェクトが operator() を介して呼び出されるたびに、格納された値は引数の1つとして渡され、もう1つの引数は operator() の引数として渡されます。結果の関数オブジェクトは単項関数となります。

1) 最初のパラメータを、オブジェクト構築時に与えられた値 value にバインドします。
2) 2番目のパラメータを、オブジェクト構築時に与えられた値 value にバインドします。

[編集]

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

出力

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)

[編集] 欠陥報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 109 C++98 operator() は渡された引数を変更できませんでした これを処理するためのオーバーロードを追加

[編集] 関連項目

(C++11で非推奨)(C++17で削除)
二項関数に一つの引数を束縛する
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)