Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/functional/not fn"

From cppreference.com
< cpp‎ | utility‎ | functional
(copy from libfun2)
 
m (fixup title)
Line 1: Line 1:
{{cpp/utility/functional/title|not_fn}}
+
{{cpp/title|not_fn}}
 
{{cpp/utility/functional/navbar}}
 
{{cpp/utility/functional/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}

Revision as of 06:16, 7 March 2016

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Function objects
Function invocation
(C++17)(C++23)
Identity function object
(C++20)
Transparent operator wrappers
(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)

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++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)

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

Contents

Parameters

f - the object from which the Template:concept object held by the wrapper is constructed

Return value

Let FD be std::decay_t<F> and fd be an lvalue of type FD constructed from std::forward<F>(f).

not_fn returns a forwarding call wrapper fn of unspecified type such that fn(a1, a2, ..., aN) is equivalent to !INVOKE(fd, a1, ..., aN), where INVOKE is the operation described in Template:concept.

The returned call wrapper is always Template:concept, and is Template:concept if FD is Template:concept.

Remarks

If fd is not Template:concept, or std::is_constructible<FD, F>::value is not true, the behavior is undefined.

Exceptions

Throws no exceptions, unless the construction of fd throws.

Possible implementation

namespace detail {
    template<class F>
    struct not_fn_t {
        F f;
        template<class... Args>
        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)...);
        }
 
        // cv-qualified overload for QoI
        template<class... Args>
        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>
        auto operator()(Args&&... args) volatile
            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>
        auto operator()(Args&&... args) const volatile
            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 F>
detail::not_fn_t<std::decay_t<F>> not_fn(F&& f) { return { std::forward<F>(f) }; }

Notes

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

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]
creates a function object that returns the complement of the result of the function object it holds
(function template) [edit]