std:: not_fn
|
定义于头文件
<functional>
|
||
|
template
<
class
F
>
/* 未指定类型 */ not_fn ( F && f ) ; |
(1) |
(C++17 起)
(C++20 起为 constexpr) |
|
template
<
auto
ConstFn
>
constexpr /* 未指定类型 */ not_fn ( ) noexcept ; |
(2) | (C++26 起) |
ConstFn
是空指针或空成员指针,则程序非良构。
目录 |
参数
| f | - | 用于构造包装器所持有的 可调用 对象的源对象 |
| 类型要求 | ||
-
std::
decay_t
<
F
>
必须满足
可调用
与
可移动构造
要求
|
||
|
-
|
||
返回值
T
的函数对象。它具有以下成员。
std::not_fn 返回类型
成员对象
std::not_fn
的返回类型持有一个类型为
std::
decay_t
<
F
>
的成员对象。
构造函数
|
explicit
T
(
F
&&
f
)
;
|
(1) |
(自 C++17 起)
(自 C++20 起为 constexpr) ( 仅用于说明* ) |
|
T
(
T
&&
f
)
=
default
;
T ( const T & f ) = default ; |
(2) | (自 C++17 起) |
|
显式默认化的定义使得返回类型不可赋值。 |
(C++20 前) |
|
未指定这些构造函数是否显式默认化,也未指定返回类型是否可赋值。 |
(自 C++20 起) |
成员函数 operator ( )
| (1) | ||
|
template
<
class
...
Args
>
auto
operator
(
)
(
Args
&&
...
args
)
&
|
(自 C++17 起)
(C++20 前) |
|
|
template
<
class
...
Args
>
constexpr
auto
operator
(
)
(
Args
&&
...
args
)
&
|
(自 C++20 起) | |
| (2) | ||
|
template
<
class
...
Args
>
auto
operator
(
)
(
Args
&&
...
args
)
&&
|
std::not_fn stateless return type
返回类型是一个 CopyConstructible 无状态类。未指定返回类型是否可赋值。
成员函数 operator ( )
|
template
<
class
...
Args
>
constexpr
auto
operator
(
)
(
Args
&&
...
args
)
const
|
(since C++26) | |
表达式等价 于 ! std:: invoke ( ConstFn, std:: forward < Args > ( args ) ... ) 。
异常
可能的实现
| (1) not_fn |
|---|
namespace detail { template<class V, class F, class... Args> constexpr bool negate_invocable_impl = false; template<class F, class... Args> constexpr bool negate_invocable_impl<std::void_t<decltype( !std::invoke(std::declval<F>(), std::declval<Args>()...))>, F, Args...> = true; template<class F, class... Args> constexpr bool negate_invocable_v = negate_invocable_impl<void, F, Args...>; template<class F> struct not_fn_t { F f; template<class... Args, std::enable_if_t<negate_invocable_v<F&, Args...>, int> = 0> constexpr decltype(auto) operator()(Args&&... args) & noexcept(noexcept(!std::invoke(f, std::forward<Args>(args)...))) { return !std::invoke(f, std::forward<Args>(args)...); } template<class... Args, std::enable_if_t<negate_invocable_v<const F&, Args...>, int> = 0> constexpr decltype(auto) operator()(Args&&... args) const& noexcept(noexcept(!std::invoke(f, std::forward<Args>(args)...))) { return !std::invoke(f, std::forward<Args>(args)...); } template<class... Args, std::enable_if_t<negate_invocable_v<F, Args...>, int> = 0> constexpr decltype(auto) operator()(Args&&... args) && noexcept(noexcept(!std::invoke(std::move(f), std::forward<Args>(args)...))) { return !std::invoke(std::move(f), std::forward<Args>(args)...); } template<class... Args, std::enable_if_t<negate_invocable_v<const F, Args...>, int> = 0> constexpr decltype(auto) operator()(Args&&... args) const&& noexcept(noexcept(!std::invoke(std::move(f), std::forward<Args>(args)...))) { return !std::invoke(std::move(f), std::forward<Args>(args)...); } // 从 C++20 起需要删除的重载 // 用于防止选择非等效但格式正确的重载 template<class... Args, std::enable_if_t<!negate_invocable_v<F&, Args...>, int> = 0> void operator(<span class |
注释
std::not_fn
旨在取代 C++03 时代的否定器
std::not1
和
std::not2
。
| 功能测试 宏 | 值 | 标准 | 功能 |
|---|---|---|---|
__cpp_lib_not_fn
|
201603L
|
(C++17) |
std::not_fn()
,
(
1
)
|
202306L
|
(C++26) |
允许将可调用对象作为常量模板参数传递给
std::not_fn
,
(
2
)
|
示例
#include <cassert> #include <functional> bool is_same(int a, int b) noexcept { return a == b; } struct S { int val; bool is_same(int arg) const noexcept { return val == arg; } }; int main() { // 使用自由函数: auto is_differ = std::not_fn(is_same); assert(is_differ(8, 8) == false); // 等价于:!is_same(8, 8) == false assert(is_differ(6, 9) == true); // 等价于:!is_same(8, 0) == true // 使用成员函数: auto member_differ = std::not_fn(&S::is_same); assert(member_differ(S{3}, 3) == false); //: S tmp{6}; !tmp.is_same(6) == false // 保留 noexcept 规范: static_assert(noexcept(is_differ) == noexcept(is_same)); static_assert(noexcept(member_differ) == noexcept(&S::is_same)); // 使用函数对象: auto same = [](int a, int b) { return a == b; }; auto differ = std::not_fn(same); assert(differ(1, 2) == true); //: !same(1, 2) == true assert(differ(2, 2) == false); //: !same(2, 2) == false #if __cpp_lib_not_fn >= 202306L auto is_differ_cpp26 = std::not_fn<is_same>(); assert(is_differ_cpp26(8, 8) == false); assert(is_differ_cpp26(6, 9) == true); auto member_differ_cpp26 = std::not_fn<&S::is_same>(); assert(member_differ_cpp26(S{3}, 3) == false); auto differ_cpp26 = std::not_fn<same>(); static_assert(differ_cpp26(1, 2) == true); static_assert(differ_cpp26(2, 2) == false); #endif }
参见
|
(deprecated in C++17)
(removed in C++20)
|
构造自定义的
std::unary_negate
对象
(函数模板) |
|
(deprecated in C++17)
(removed in C++20)
|
构造自定义的
std::binary_negate
对象
(函数模板) |