Namespaces
Variants

std:: unary_function

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
unary_function
( 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* )
( 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 < typename ArgumentType, typename ResultType >
struct unary_function ;
(C++11で非推奨)
(C++17で削除)

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

std::unary_function operator ( ) を定義しません。派生クラスがこれを定義することが期待されています。 std::unary_function が提供するのは、テンプレートパラメータによって定義される2つの型 - argument_type result_type のみです。

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

std::unary_function はC++11で非推奨となりました。

メンバー型

定義
argument_type ArgumentType
result_type ResultType

#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

関連項目

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