Namespaces
Variants

std:: binary_function

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* )
binary_function
( 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* )
( 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* )
ヘッダーで定義 <functional>
template <

class Arg1,
class Arg2,
class Result

> struct binary_function ;
(C++11で非推奨)
(C++17で削除)

std::binary_function は、2つの引数を持つ関数オブジェクトを作成するための基底クラスです。

std::binary_function operator ( ) を定義していない。派生クラスがこれを定義することが期待されている。 std::binary_function が提供するのは、テンプレートパラメータによって定義される3つの型 - first_argument_type second_argument_type および result_type のみである。

一部の標準ライブラリ関数オブジェクトアダプタ、例えば std::not2 は、適応対象の関数オブジェクトが特定の型を定義していることを要求します。 std::not2 は、適応される関数オブジェクトが first_argument_type second_argument_type という二つの型を持つことを要求します。二つの引数を取る関数オブジェクトを std::binary_function から派生させることは、それらをこれらのアダプタと互換性を持たせる簡単な方法です。

std::binary_function はC++11で非推奨となり、C++17で削除されました。

メンバー型

定義
first_argument_type Arg1
second_argument_type Arg2
result_type Result

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
struct same : std::binary_function<int, int, bool>
{
    bool operator()(int a, int b) const { return a == b; }
};
int main()
{
    std::vector<char> v1{'A', 'B', 'C', 'D', 'E'};
    std::vector<char> v2{'E', 'D', 'C', 'B', 'A'};
    std::vector<bool> v3(v1.size());
    std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), std::not2(same()));
    std::cout << std::boolalpha;
    for (std::size_t i = 0; i < v1.size(); ++i)
        std::cout << v1[i] << " != " << v2[i] << " : " << v3[i] << '\n';
}

出力:

A != E : true
B != D : true
C != C : false
D != B : true
E != A : true

関連項目

(C++11)
任意のコピー構築可能な呼び出し可能オブジェクトのコピー可能ラッパー
(クラステンプレート)
指定された呼び出しシグネチャで修飾子をサポートする任意の呼び出し可能オブジェクトのムーブ専用ラッパー
(クラステンプレート)
(C++11で非推奨) (C++17で削除)
関数ポインタからアダプタ互換な関数オブジェクトラッパーを作成する
(関数テンプレート)
(C++11で非推奨) (C++17で削除)
二項関数へのポインタのアダプタ互換ラッパー
(クラステンプレート)
(C++11で非推奨) (C++17で削除)
アダプタ互換な単項関数の基底クラス
(クラステンプレート)