名前空間
変種
操作

std::binder1st, std::binder2nd

提供: cppreference.com
< cpp‎ | utility‎ | functional
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ (C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
関数オブジェクト
関数ラッパー
(C++11)
(C++11)
関数の部分適用
(C++20)
(C++11)
関数呼び出し
(C++17)
恒等関数オブジェクト
(C++20)
参照ラッパー
(C++11)(C++11)
演算子ラッパー
否定子
(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 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() を通して関数オブジェクトが呼ばれると、格納された値が引数のひとつとして渡され、他の引数は operator() の引数として渡されます。 結果の関数オブジェクトは単項関数です。

1) 第1引数をオブジェクトの構築時に与えられた値 value に束縛します。
2) 第2引数をオブジェクトの構築時に与えられた値 value に束縛します。

[編集]

#include <iostream>
#include <functional>
#include <cmath>
#include <vector>
const double pi = std::acos(-1);
int main()
{
    // C++11 で非推奨になり C++17 で削除された古い手法。
    std::binder1st<std::multiplies<double>> f1 = std::bind1st(
                                                   std::multiplies<double>(), pi / 180.);
 
    // C++11 では以下のようにします。
    auto f2 = [](double a){ return a*pi/180.; };
 
    for(double n : {0, 30, 45, 60, 90, 180})
        std::cout << n << " deg = " << f1(n) << " rad (using binder) "
                                    << f2(n) << " rad (using lambda)\n";
}

出力:

0 deg = 0 rad (using binder) 0 rad (using lambda)
30 deg = 0.523599 rad (using binder) 0.523599 rad (using lambda)
45 deg = 0.785398 rad (using binder) 0.785398 rad (using lambda)
60 deg = 1.0472 rad (using binder) 1.0472 rad (using lambda)
90 deg = 1.5708 rad (using binder) 1.5708 rad (using lambda)
180 deg = 3.14159 rad (using binder) 3.14159 rad (using lambda)

[編集] 関連項目

(C++11で非推奨)(C++17で削除)
二項関数に引数を1つ束縛します
(関数テンプレート) [edit]