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) 보유한 호출 가능 객체의 부정(negation)을 반환하는 전달 호출 래퍼를 생성합니다.
2) 정적으로 결정된 호출 가능 대상의 부정(negation)을 반환하는 전달 호출 래퍼를 생성합니다. 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 요구사항을 충족해야 하므로, 반환된 호출 래퍼(call wrapper)는 항상 MoveConstructible 이며, std:: decay_t < F > CopyConstructible 인 경우에만 CopyConstructible 입니다.

명시적으로 기본 정의된 생성자들로 인해 반환 타입은 할당 가능(assignable)하지 않습니다.

(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를 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 stateless return type

반환 타입은 CopyConstructible stateless 클래스입니다. 반환 타입이 assignable인지 여부는 명시되지 않습니다.

멤버 함수 operator ( )

template < class ... Args >

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

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

Expression-equivalent ! 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()(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 객체를 생성합니다
(함수 템플릿)