std::binder1st, std::binder2nd
| ヘッダ <functional> で定義 |
||
| template< class Fn > class binder1st |
(1) | (C++11で非推奨) (C++17で削除) |
| template< class Fn > class binder2nd |
(2) | (C++11で非推奨) (C++17で削除) |
二項関数に引数をバインドする関数オブジェクト。
パラメータの値は、オブジェクト構築時にオブジェクトに渡され、オブジェクト内に格納されます。関数オブジェクトが operator() を介して呼び出されるたびに、格納された値は引数の1つとして渡され、もう1つの引数は operator() の引数として渡されます。結果の関数オブジェクトは単項関数となります。
[編集] 例
#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で削除) |
二項関数に一つの引数を束縛する (関数テンプレート) |