std::not_fn
| 定义于头文件 <functional>
|
||
| template< class F> /*unspecified*/ not_fn( F&& f ); |
(C++17 起) (C++20 前) |
|
| template< class F> constexpr /*unspecified*/ not_fn( F&& f ); |
(C++20 起) | |
构造一个转发调用包装器,返回其所保有的可调用对象的逻辑非。
参数
| f | - | 构造包装器所保有的可调用 (Callable) 对象的来源对象 |
| 类型要求 | ||
-std::decay_t<F> 必须满足可调用 (Callable) 和 可移动构造 (MoveConstructible) 的要求。
| ||
| -要求 std::is_constructible_v<std::decay_t<F>, F> 为 true | ||
返回值
未指定类型 T 的函数对象。它拥有下列成员:
std::not_fn 返回类型
成员对象
std::not_fn 的返回类型保有一个 std::decay_t<F> 类型的成员对象。
构造函数
| (1) | ||
| explicit T(F&& f); // 仅用于阐释 |
(C++17 起) (C++20 前) |
|
| explicit constexpr T(F&& f); // 仅用于阐释 |
(C++20 起) | |
| T(T&& f) = default; T(const T& f) = default; |
(2) | |
成员函数 operator()
| (1) | ||
| template<class... Args> auto operator()(Args&&... args) & -> decltype( |
(C++17 起) (C++20 前) |
|
| template<class... Args> constexpr auto operator()(Args&&... args) & noexcept(/*see below*/) |
(C++20 起) | |
| (2) | ||
| template<class... Args> auto operator()(Args&&... args) && -> decltype( |
(C++17 起) (C++20 前) |
|
| template<class... Args> constexpr auto operator()(Args&&... args) && noexcept(/*see below*/) |
(C++20 起) | |
|
1) 等价于 return !std::invoke(fd, std::forward<Args>(args)...)
2) 等价于 return !std::invoke(std::move(fd), std::forward<Args>(args)...)
|
(C++17 起) (C++20 前) |
|
1) 表达式等价于 !std::invoke(fd, std::forward<Args>(args)...)
2) 表达式等价于 !std::invoke(std::move(fd), std::forward<Args>(args)...)
|
(C++20 起) |
其中 fd 是 std::decay_t<F> 类型的成员对象
异常
不抛出异常,除非 fd 的构造抛出异常。
可能的实现
namespace detail { template<class F> struct not_fn_t { F f; template<class... Args> constexpr auto operator()(Args&&... args) & noexcept(noexcept(!std::invoke(f, std::forward<Args>(args)...))) -> decltype(!std::invoke(f, std::forward<Args>(args)...)) { return !std::invoke(f, std::forward<Args>(args)...); } template<class... Args> constexpr auto operator()(Args&&... args) const& noexcept(noexcept(!std::invoke(f, std::forward<Args>(args)...))) -> decltype(!std::invoke(f, std::forward<Args>(args)...)) { return !std::invoke(f, std::forward<Args>(args)...); } template<class... Args> constexpr auto operator()(Args&&... args) && noexcept(noexcept(!std::invoke(std::move(f), std::forward<Args>(args)...))) -> decltype(!std::invoke(std::move(f), std::forward<Args>(args)...)) { return !std::invoke(std::move(f), std::forward<Args>(args)...); } template<class... Args> constexpr auto operator()(Args&&... args) const&& noexcept(noexcept(!std::invoke(std::move(f), std::forward<Args>(args)...))) -> decltype(!std::invoke(std::move(f), std::forward<Args>(args)...)) { return !std::invoke(std::move(f), std::forward<Args>(args)...); } }; } template<class F> constexpr detail::not_fn_t<std::decay_t<F>> not_fn(F&& f) { return { std::forward<F>(f) }; } |
注解
not_fn 的目的是取代 C++03 时代的取反器 std::not1 及 std::not2 。
示例
| 本节未完成 原因:暂无示例 |
参阅
| (C++17 中弃用)(C++20 中移除) |
构造定制的 std::unary_negate 对象 (函数模板) |
| (C++17 中弃用)(C++20 中移除) |
构造定制的 std::binary_negate 对象 (函数模板) |