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 | - | 用于构造包装器所持有的 Callable 对象的源对象 |
| 类型要求 | ||
-
std::
decay_t
<
F
>
必须满足
Callable
与
MoveConstructible
的要求
|
||
|
-
|
||
返回值
T
. It has the following members.
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
)
&&
|
(自 C++17 起)
(至 C++20 止) |
|
|
template
<
class
...
Args
>
constexpr
auto
operator
(
)
(
Args
&&
...
args
)
&&
|
(自 C++20 起) | |
令 fd 作为类型为 std:: decay_t < F > 的成员对象。
|
1)
等价于
return
!
std::
invoke
(
fd,
std::
forward
<
Args
>
(
args
)
...
)
;
2)
等价于
return
!
std::
invoke
(
std
::
move
(
fd
)
,
std::
forward
<
Args
>
(
args
)
...
)
;
在调用结果时,如果对原始选择的 operator ( ) 重载的返回类型进行替换失败, 可能会选择其他重载 。 |
(C++17 起)
(C++20 前) |
|
在调用结果时,如果对原始选择的 operator ( ) 重载的返回类型进行替换失败,则调用非良构,这也可能构成 替换失败 。 |
(C++20 起) |
std::not_fn 无状态返回类型
返回类型是一个 CopyConstructible 无状态类。未指定返回类型是否可赋值。
成员函数 operator ( )
|
template
<
class
...
Args
>
constexpr
auto
operator
(
)
(
Args
&&
...
args
)
const
|
(自 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()(Args&&...) & = delete; template<class... Args, std::enable_if_t<!negate_invocable_v<const F&, Args...>, int> = 0> void operator()(Args&&...) const& = delete; template<class... Args, std::enable_if_t<!negate_invocable_v<F, Args...>, int> = 0> void operator()(Args&&...) && = delete; template<class... Args, std::enable_if_t<!negate_invocable_v<const F, Args...>, int> = 0> void operator()(Args&&...) const&& = delete; }; } template<class F> constexpr detail::not_fn_t<std::decay_t<F>> not_fn(F&& f) { return {std::forward<F>(f)}; } |
| (2) not_fn |
namespace detail { template<auto ConstFn> struct stateless_not_fn { template<class... Args> constexpr auto operator()(Args&&... args) const noexcept(noexcept(!std::invoke(ConstFn, std::forward<Args>(args)...))) -> decltype(!std::invoke(ConstFn, std::forward<Args>(args)...)) { return !std::invoke(ConstFn, std::forward<Args>(args)...); } }; } template<auto ConstFn> constexpr detail::stateless_not_fn<ConstFn> not_fn() noexcept { if constexpr (std::is_pointer_v<decltype(ConstFn)> || std::is_member_pointer_v<decltype(ConstFn)>) static_assert(ConstFn != nullptr); return {}; } |
注释
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
对象
(函数模板) |