Namespaces
Variants

std:: not_fn

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
定义于头文件 <functional>
template < class F >
/* 未指定类型 */ not_fn ( F && f ) ;
(1) (C++17 起)
(C++20 起为 constexpr)
template < auto ConstFn >
constexpr /* 未指定类型 */ not_fn ( ) noexcept ;
(2) (C++26 起)
1) 创建一个转发调用包装器,该包装器返回其所持有的可调用对象的逻辑非结果。
2) 创建一个转发调用包装器,返回静态确定的可调用目标的逻辑否定。如果 ConstFn 是空指针或空成员指针,则程序非良构。

目录

参数

f - 用于构造包装器所持有的 可调用 对象的源对象
类型要求
-
std:: decay_t < F > 必须满足 可调用 可移动构造 要求
-
std:: is_constructible_v < std:: decay_t < F > , F > 必须为 true

返回值

1) 一个未指定类型 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 起)
1) 该构造函数直接非列表初始化成员对象(类型为 std:: decay_t < F > )从 std:: forward < F > ( f ) 。抛出所选构造函数抛出的任何异常。
2) 因为 std:: decay_t < F > 需要满足 可移动构造 ,返回的调用包装器始终是 可移动构造的 ,并且如果 std:: decay_t < F > 可复制构造的 ,则它也是 可复制构造的

显式默认化的定义使得返回类型不可赋值。

(C++20 前)

未指定这些构造函数是否显式默认化,也未指定返回类型是否可赋值。

(自 C++20 起)

成员函数 operator ( )

(1)
template < class ... Args >

auto operator ( ) ( Args && ... args ) &
- > decltype ( ! std:: declval <
std:: invoke_result_t < std:: decay_t < F > & , Args... >> ( ) ) ;
template < class ... Args >
auto operator ( ) ( Args && ... args ) const &
- > decltype ( ! std:: declval <

std:: invoke_result_t < std:: decay_t < F > const & , Args... >> ( ) ) ;
(自 C++17 起)
(C++20 前)
template < class ... Args >

constexpr auto operator ( ) ( Args && ... args ) &
noexcept ( /* 见下文 */ )
- > decltype ( ! std:: invoke (
std:: declval < std:: decay_t < F > & > ( ) , std:: declval < Args > ( ) ... ) ) ;
template < class ... Args >
constexpr auto operator ( ) ( Args && ... args ) const &
noexcept ( /* 见下文 */ )
- > decltype ( ! std:: invoke (

std:: declval < std:: decay_t < F > const & > ( ) , std:: declval < Args > ( ) ... ) ) ;
(自 C++20 起)
(2)
template < class ... Args >

auto operator ( ) ( Args && ... args ) &&
- > decltype ( ! std:: declval <
std:: invoke

2) 以下类型的值。

std::not_fn stateless return type

返回类型是一个 CopyConstructible 无状态类。未指定返回类型是否可赋值。

成员函数 operator ( )

template < class ... Args >

constexpr auto operator ( ) ( Args && ... args ) const
noexcept ( /* see below */ )

- > decltype ( ! std:: invoke ( ConstFn, std:: declval < Args > ( ) ... ) ) ;
(since C++26)

表达式等价 ! std:: invoke ( ConstFn, std:: forward < Args > ( args ) ... )

异常

1) 不抛出异常,除非 fd 的构造过程抛出异常。

可能的实现

(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 对象
(函数模板)