Namespaces
Variants
Views
Actions

std::not_fn

From cppreference.com
< cpp‎ | utility‎ | functional
 
 
 
Function objects
Function wrappers
(C++11)
(C++11)
Partial function application
(C++20)
(C++11)
Function invocation
(C++17)
Identity function object
(C++20)
Reference wrappers
(C++11)(C++11)
Operator wrappers
Negators
not_fn
(C++17)
Searchers
Constrained comparators
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)
 
Defined in header <functional>
template< class F>
/*unspecified*/ not_fn( F&& f );
(since C++17)
(until C++20)
template< class F>
constexpr /*unspecified*/ not_fn( F&& f );
(since C++20)

Creates a forwarding call wrapper that returns the negation of the callable object it holds.

Contents

[edit] Parameters

f - the object from which the Callable object held by the wrapper is constructed
Type requirements
-
std::decay_t<F> must meet the requirements of Callable and MoveConstructible.
-
std::is_constructible_v<std::decay_t<F>, F> is required to be true

[edit] Return value

A function object of unspecified type T. It has the following members:

std::not_fn return type

Member objects

The return type of std::not_fn holds a member object of type std::decay_t<F>.

Constructors

(1)
explicit T(F&& f); // exposition only
(since C++17)
(until C++20)
explicit constexpr T(F&& f); // exposition only
(since C++20)
T(T&& f) = default;
T(const T& f) = default;
(2)
1) The constructor direct-non-list-initializes the member object (of type std::decay_t<F>) from std::forward<F>(f). Throws any exception thrown by the constructor selected
2) Because std::decay_t<F> is required to be MoveConstructible, the returned call wrapper is always MoveConstructible, and is CopyConstructible if std::decay_t<F> is CopyConstructible.

Member function 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...>>());
(since C++17)
(until C++20)
template<class... Args> constexpr auto operator()(Args&&... args) &

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

    !std::declval<std::invoke_result_t<std::decay_t<F> const&, Args...>>());
(since 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...>>());
(since C++17)
(until C++20)
template<class... Args> constexpr auto operator()(Args&&... args) &&

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

    !std::declval<std::invoke_result_t<std::decay_t<F> const, Args...>>());
(since C++20)
1) Equivalent to return !std::invoke(fd, std::forward<Args>(args)...);
2) Equivalent to return !std::invoke(std::move(fd), std::forward<Args>(args)...);
(since C++17)
(until C++20)
1) Expression-equivalent to !std::invoke(fd, std::forward<Args>(args)...)
2) Expression-equivalent to !std::invoke(std::move(fd), std::forward<Args>(args)...)
(since C++20)

where fd is the member object of type std::decay_t<F>

[edit] Expression-equivalent

Expression e is expression-equivalent to expression f, if e and f have the same effects, either are both potentially-throwing or are both not potentially-throwing (i.e. noexcept(e) == noexcept(f)), and either are both constant subexpressions or are both not constant subexpressions.

[edit] Exceptions

Throws no exceptions, unless the construction of fd throws.

[edit] Possible implementation

namespace detail {
    template<class F>
    struct not_fn_t {
        F f;
        template<class... Args>
        constexpr auto operator()(Args&&... args) &
            noexcept(noexcept(!std::invoke(f, std::forward<Args>(args)...)))
            -> decltype(!std::invoke(f, std::forward<Args>(args)...))
        {
            return !std::invoke(f, std::forward<Args>(args)...);
        }
 
        template<class... Args>
        constexpr auto operator()(Args&&... args) const&
            noexcept(noexcept(!std::invoke(f, std::forward<Args>(args)...)))
            -> decltype(!std::invoke(f, std::forward<Args>(args)...))
        {
            return !std::invoke(f, std::forward<Args>(args)...);
        }
 
        template<class... Args>
        constexpr auto operator()(Args&&... args) &&
            noexcept(noexcept(!std::invoke(std::move(f), std::forward<Args>(args)...)))
            -> decltype(!std::invoke(std::move(f), std::forward<Args>(args)...))
        {
            return !std::invoke(std::move(f), std::forward<Args>(args)...);
        }
 
        template<class... Args>
        constexpr auto operator()(Args&&... args) const&&
            noexcept(noexcept(!std::invoke(std::move(f), std::forward<Args>(args)...)))
            -> decltype(!std::invoke(std::move(f), std::forward<Args>(args)...))
        {
            return !std::invoke(std::move(f), std::forward<Args>(args)...);
        }
    };
}
 
template<class F>
constexpr detail::not_fn_t<std::decay_t<F>> not_fn(F&& f)
{
    return { std::forward<F>(f) };
}

[edit] Notes

not_fn is intended to replace the C++03-era negators std::not1 and std::not2.

[edit] Example

[edit] See also

(deprecated in C++17)(removed in C++20)
constructs custom std::unary_negate object
(function template) [edit]
(deprecated in C++17)(removed in C++20)
constructs custom std::binary_negate object
(function template) [edit]