std::ptr_fun
来自cppreference.com
< cpp | utility | functional
在标头 <functional> 定义
|
||
template< class Arg, class Result > std::pointer_to_unary_function<Arg,Result> |
(1) | (C++11 中弃用) (C++17 中移除) |
template< class Arg1, class Arg2, class Result > std::pointer_to_binary_function<Arg1,Arg2,Result> |
(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))); // C++11 替用方案: std::not1(std::cref(is_vowel))); std::not1(std::function<bool(char)>(is_vowel))); [](char c) { return !is_vowel(c); }); // C++17 替用方案: std::not_fn(is_vowel)); #endif }
输出:
Hll, wrld!
参阅
(C++11) |
任意可复制构造的可调用对象的可复制包装 (类模板) |
(C++23) |
任意可调用对象的仅移动包装,支持给定调用签名中的限定符 (类模板) |
(C++17)(C++23) |
以给定实参和可能指定的返回类型 (C++23 起)调用任意可调用 (Callable) 对象 (函数模板) |
(C++17) |
创建返回其保有的函数对象的结果之补的函数对象 (函数模板) |