functional_binary_function

functional_binary_function (이진 함수 기반 클래스)

std::binary_function은 두 개의 인자를 받는 함수 객체를 만들 때 사용하는 기반 클래스예요. 이 클래스는 operator()를 직접 정의하지 않으며, 파생 클래스가 이를 구현하도록 기대해요. 대신 first_argument_type, second_argument_type, result_type이라는 세 가지 타입을 제공해서 함수 객체가 어댑터와 호환되도록 도와줘요.

출처: cppreference

본문

std::binary_function<functional> 헤더에 정의되어 있어요. C++11에서 더 이상 사용하지 않게(deprecated) 되었고, C++17에서 완전히 제거되었어요.

template < class Arg1 , class Arg2 , class Result > struct binary_function ;

이 클래스는 두 개의 인자를 받는 함수 객체를 위한 기반 클래스예요. std::binary_functionoperator()를 정의하지 않아요. 파생 클래스가 이 연산자를 정의해야 하죠. std::binary_function은 템플릿 매개변수로부터 first_argument_type, second_argument_type, result_type이라는 세 가지 타입만 제공해요.

일부 표준 라이브러리 함수 객체 어댑터(예: std::not2)는 적응 대상 함수 객체에 특정 타입이 정의되어 있어야 해요. std::not2는 적응되는 함수 객체가 first_argument_typesecond_argument_type이라는 두 타입을 가지고 있어야 해요. 두 인자를 받는 함수 객체를 std::binary_function에서 파생시키면 이런 어댑터와 쉽게 호환할 수 있어요.

std::binary_function은 C++11에서 deprecated 되었고, 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

같이 보기

함수 설명
function (C++11) 복사 생성 가능한 호출 가능 객체의 복사 가능 래퍼 (클래스 템플릿)
move_only_function (C++23) 주어진 호출 시그니처에서 한정자를 지원하는 모든 호출 가능 객체의 이동 전용 래퍼 (클래스 템플릿)
ptr_fun (C++11에서 deprecated, C++17에서 제거) 함수 포인터로부터 어댑터 호환 함수 객체 래퍼를 생성 (함수 템플릿)
pointer_to_binary_function (C++11에서 deprecated, C++17에서 제거) 이진 함수 포인터를 위한 어댑터 호환 래퍼 (클래스 템플릿)
unary_function (C++11에서 deprecated, C++17에서 제거) 단항 함수 기반 클래스 (클래스 템플릿)

더 알아보기 (Learn more)

cppreference