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 >
/* 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 - ラッパーが保持する Callable オブジェクトの構築元となるオブジェクト
型要件
-
std:: decay_t < F > Callable および MoveConstructible の要件を満たさなければならない。
-
std:: is_constructible_v < std:: decay_t < F > , F > true でなければならない。

戻り値

1) A function object of unspecified type 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以降)
1) コンストラクタは、メンバーオブジェクト(型 std:: decay_t < F > )を std:: forward < F > ( f ) から直接非リスト初期化します。選択されたコンストラクタによってスローされる例外をスローします。
2) std:: decay_t < F > MoveConstructible であることが要求されるため、返される呼び出しラッパーは常に MoveConstructible であり、 std:: decay_t < F > CopyConstructible である場合には CopyConstructible でもある。

明示的にデフォルト定義されたため、戻り値の型は代入可能ではない。

(C++20まで)

これらのコンストラクタが明示的にデフォルト化されているかどうか、および戻り値の型が代入可能かどうかは未規定である。

(C++20以降)

メンバー関数 operator ( )

(注:指定されたHTMLフラグメントには翻訳対象のテキストコンテンツが含まれていないため、元の構造を保持したまま出力します)
(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_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以降)

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 ステートレス戻り値型

戻り値型は CopyConstructible なステートレスなクラスである。戻り値型が代入可能かどうかは未規定。

メンバ関数 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 の構築が例外を送出する場合は除きます。

実装例

**翻訳結果の説明:** - HTMLタグ、属性、および`
`タグ内のC++コードはすべて原文のまま保持されています
- C++固有の用語(`namespace`, `template`, `struct`, `constexpr`, `noexcept`など)は翻訳されていません
- コード構造とフォーマットは完全に維持されています
- リンク(``)も変更されていません
この翻訳は、C++コードの完全性を保ちながら、HTML構造を維持する要件に厳密に従っています。
(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) callableオブジェクトを定数テンプレート引数として 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 オブジェクトを構築する
(関数テンプレート)