functional_unary_function

functional_unary_function (단항 함수 기본 클래스)

std::unary_function은 인자를 하나 받는 함수 객체를 만들기 위한 기본 클래스예요. 이 타입은 C++11에서 더 이상 사용되지 않게 되었고, C++17에서 제거되었어요. 파생 클래스에서 operator()를 정의하는 방식으로 사용해요.

출처: cppreference

본문

<functional> 헤더에 정의되어 있어요.

템플릿 정의 상태
template < typename ArgumentType, typename ResultType > struct unary_function; (deprecated in C++11) (removed in C++17)

std::unary_function은 인자를 하나 받는 함수 객체를 생성하기 위한 기본 클래스예요.

std::unary_function은 operator()를 정의하지 않아요. 파생 클래스에서 이 연산자를 정의할 것으로 기대해요. std::unary_function은 템플릿 매개변수로 정의되는 두 가지 타입, argument_type과 result_type만 제공해요.

일부 표준 라이브러리 함수 객체 어댑터(예: std::not1)는 적응되는 함수 객체에 특정 타입이 정의되어 있어야 해요. std::not1은 적응되는 함수 객체가 argument_type이라는 이름의 타입을 가질 것을 요구해요. 인자를 하나 받는 함수 객체를 std::unary_function에서 파생시키는 것은 그러한 어댑터와 호환되도록 만드는 쉬운 방법이에요.

std::unary_function은 C++11에서 더 이상 사용되지 않아요(deprecated).

멤버 타입 (Member types)

타입 정의
argument_type ArgumentType
result_type ResultType

예제 (Example)

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

struct less_than_7 : std::unary_function<int, bool>
{
    bool operator()(int i) const { return i < 7; }
};

int main()
{
    std::vector<int> v(10, 7);
    v[0] = v[1] = v[2] = 6;
 
    std::cout << std::count_if(v.begin(), v.end(), std::not1(less_than_7()));
 
    // C++11 solution:
    // Cast to std::function<bool (int)> somehow - even with a lambda
    // std::cout << std::count_if(v.begin(), v.end(),
    //     std::not1(std::function<bool (int)>([](int i) { return i < 7; })));
}

출력:

7

같이 보기 (See also)

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

더 알아보기 (Learn more)

cppreference