名稱空間
變體
操作

std::ptr_fun

來自 cppreference.com
 
 
 
函式物件
函式呼叫
(C++17)(C++23)
恆等函式物件
(C++20)
透明運算子包裝器
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

舊繫結器和介面卡
(直到 C++17*)
(直到 C++17*)
ptr_fun
(直到 C++17*)
(直到 C++17*)  
(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 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_functionstd::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::functionstd::ref,它們都可以從普通函式建立相容可呼叫介面卡的函式物件。

目錄

[edit] 引數

f - 指向要為其建立包裝器的函式的指標

[edit] 返回值

包裝 f 的函式物件。

[編輯] 異常

可能丟擲實現定義的異常。

[edit] 示例

#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!

[edit] 參見

(C++11)
任何可複製構造的可呼叫物件的包裝器
(類模板) [edit]
任何支援給定呼叫簽名中限定符的可呼叫物件的僅移動包裝器
(類模板) [edit]
(C++17)(C++23)
以給定引數呼叫任何 Callable 物件並可指定返回型別(從 C++23 開始)
(函式模板) [edit]
(C++17)
建立函式物件,該物件返回其所持函式物件結果的補數
(函式模板) [edit]