名稱空間
變體
操作

std::not_fn

來自 cppreference.com
 
 
 
函式物件
函式呼叫
(C++17)(C++23)
恆等函式物件
(C++20)
透明運算子包裝器
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

舊繫結器和介面卡
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)  
(直到 C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(直到 C++20*)
(直到 C++20*)
 
定義於標頭檔案 <functional>
template< class F >
/* unspecified */ not_fn( F&& f );
(1) (C++17 起)
(C++20 起為 constexpr)
template< auto ConstFn >
constexpr /* unspecified */ 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(/* see below */)
-> decltype(!std::invoke(
    std::declval<std::decay_t<F>&>(), std::declval<Args>()...));
template< class... Args >
constexpr auto operator()( Args&&... args ) const&
    noexcept(/* see below */)
-> 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_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(/* see below */)
-> decltype(!std::invoke(
    std::declval<std::decay_t<F>>(), std::declval<Args>()...));
template< class... Args >
constexpr auto operator()( Args&&... args ) const&&
    noexcept(/* see below */)
-> decltype(!std::invoke(

    std::declval<std::decay_t<F> const>(), std::declval<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 前)
1) 表示式等價!std::invoke(fd, std::forward<Args>(args)...)
2) 表示式等價!std::invoke(std::move(fd), std::forward<Args>(args)...)

在呼叫結果時,如果最初選擇的 operator() 過載的返回型別替換失敗,則呼叫格式錯誤,這也可能導致替換失敗

(C++20 起)
2) 以下型別的值。

std::not_fn 無狀態返回型別

返回型別是可複製構造的無狀態類。未指定返回型別是否可賦值。

成員函式 operator()

template< class... Args >

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

-> decltype(!std::invoke(ConstFn, std::declval<Args>()...));
(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)...);
        }
 
        // Deleted overloads are needed since C++20
        // for preventing a non-equivalent but well-formed overload to be selected.
 
        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::not1std::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()
{
    // Using with a free function:
    auto is_differ = std::not_fn(is_same);
    assert(is_differ(8, 8) == false); // equivalent to: !is_same(8, 8) == false
    assert(is_differ(6, 9) == true); // equivalent to: !is_same(8, 0) == true
 
    // Using with a member function:
    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-specification is preserved:
    static_assert(noexcept(is_differ) == noexcept(is_same));
    static_assert(noexcept(member_differ) == noexcept(&S::is_same));
 
    // Using with a function object:
    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
}

[編輯] 參閱

(C++17 中已棄用)(C++20 中已移除)
構造自定義的 std::unary_negate 物件
(函式模板) [編輯]
(C++17 中已棄用)(C++20 中已移除)
構造自定義的 std::binary_negate 物件
(函式模板) [編輯]