命名空间
变体
操作

std::experimental::not_fn

来自 cppreference.cn
定义于头文件 <experimental/functional>
template< class F>
/*unspecified*/ not_fn( F&& f );
(library fundamentals TS v2) (库基础 TS v2)

创建一个转发调用包装器,该包装器返回其持有的可调用对象的补码。

目录

[编辑] 参数

f - 从中构造包装器持有的 Callable 对象

[编辑] 返回值

FDstd::decay_t<F>fd 为类型 FD 的左值,从 std::forward<F>(f) 构造而来。

not_fn 返回一个未指定类型的转发调用包装器 fn,使得 fn(a1, a2, ..., aN) 等价于 !INVOKE(fd, a1, ..., aN),其中 INVOKE 是在 Callable 中描述的操作。

返回的调用包装器始终是 MoveConstructible (可移动构造的),并且如果 FD 是 CopyConstructible (可复制构造的),则也是 CopyConstructible (可复制构造的)。

[编辑] 备注

如果 fd 不是 Callable (可调用对象),或者 std::is_constructible<FD, F>::value 不为 true,则行为未定义。

[编辑] 异常

不抛出异常,除非 fd 的构造抛出异常。

[编辑] 可能的实现

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) }; }

[编辑] 注释

not_fn 旨在替代 C++03 时代的否定器 std::not1std::not2

[编辑] 参见

(C++17)
创建一个函数对象,该对象返回其持有的函数对象的结果的补码
(函数模板) [编辑]
Retrieved from "https://cppreference.cn/mwiki/index.php?title=cpp/experimental/not_fn&oldid=103392" (检索自“https://cppreference.cn/mwiki/index.php?title=cpp/experimental/not_fn&oldid=103392“)