Namespaces
Variants

std:: ptr_fun

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* )
( until C++17* )
ptr_fun
( 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 Arg, class Result >

std:: pointer_to_unary_function < Arg,Result >

ptr_fun ( Result ( * f ) ( Arg ) ) ;
(1) (C++11で非推奨)
(C++17で削除)
template < class Arg1, class Arg2, class Result >

std:: pointer_to_binary_function < Arg1,Arg2,Result >

ptr_fun ( Result ( * f ) ( Arg1, Arg2 ) ) ;
(2) (C++11で非推奨)
(C++17で削除)

関数ラッパーオブジェクト( std:: pointer_to_unary_function または std:: pointer_to_binary_function )を作成し、テンプレート引数から対象の型を推論します。

1) 実質的に std:: pointer_to_unary_function < Arg,Result > ( f ) を呼び出します。
2) 実質的に std:: pointer_to_binary_function < Arg1,Arg2,Result > ( f ) を呼び出します。

この関数および関連する型は、C++11以降、より汎用的な std::function および std::ref を推奨するため非推奨となりました。これらはいずれも、通常の関数から呼び出し可能なアダプタ互換の関数オブジェクトを生成します。

目次

パラメータ

f - ラッパーを作成する関数へのポインタ

戻り値

f をラップする関数オブジェクト。

例外

実装定義の例外をスローする可能性があります。

#include <algorithm>
#include <functional>
#include <iostream>
#include <string_view>
constexpr bool is_vowel(char c)
{
    return std::string_view{"aeoiuAEIOU"}.find(c) != std::string_view::npos;
}
int main()
{
    std::string_view s = "Hello, world!";
    std::ranges::copy_if(s, std::ostreambuf_iterator<char>(std::cout),
        std::not1(std::ptr_fun(is_vowel)));
#if 0
// C++11 alternatives:
        std::not1(std::cref(is_vowel)));
        std::not1(std::function<bool(char)>(is_vowel)));
        [](char c) { return !is_vowel(c); });
// C++17 alternatives:
        std::not_fn(is_vowel));
#endif
}

出力:

Hll, wrld!

関連項目

(C++11)
任意のコピー構築可能な呼び出し可能オブジェクトのコピー可能ラッパー
(クラステンプレート)
指定された呼び出しシグネチャで修飾子をサポートする任意の呼び出し可能オブジェクトのムーブ専用ラッパー
(クラステンプレート)
(C++17) (C++23)
任意の Callable オブジェクトを指定された引数で呼び出す (戻り値の型を指定可能) (C++23以降)
(関数テンプレート)
(C++17)
保持する関数オブジェクトの結果の補数を返す関数オブジェクトを作成する
(関数テンプレート)