xref: /llvm-project/libcxx/include/__functional/not_fn.h (revision c91d805e6627987bec8ec34ea67c1e8240940039)
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___FUNCTIONAL_NOT_FN_H
11 #define _LIBCPP___FUNCTIONAL_NOT_FN_H
12 
13 #include <__config>
14 #include <__functional/invoke.h>
15 #include <__functional/perfect_forward.h>
16 #include <__type_traits/decay.h>
17 #include <__type_traits/enable_if.h>
18 #include <__type_traits/is_constructible.h>
19 #include <__type_traits/is_member_pointer.h>
20 #include <__type_traits/is_pointer.h>
21 #include <__utility/forward.h>
22 
23 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
24 #  pragma GCC system_header
25 #endif
26 
27 _LIBCPP_BEGIN_NAMESPACE_STD
28 
29 #if _LIBCPP_STD_VER >= 17
30 
31 struct __not_fn_op {
32   template <class... _Args>
33   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 auto operator()(_Args&&... __args) const
34       noexcept(noexcept(!std::invoke(std::forward<_Args>(__args)...)))
35           -> decltype(!std::invoke(std::forward<_Args>(__args)...)) {
36     return !std::invoke(std::forward<_Args>(__args)...);
37   }
38 };
39 
40 template <class _Fn>
41 struct __not_fn_t : __perfect_forward<__not_fn_op, _Fn> {
42   using __perfect_forward<__not_fn_op, _Fn>::__perfect_forward;
43 };
44 
45 template <class _Fn,
46           class = enable_if_t< is_constructible_v<decay_t<_Fn>, _Fn> && is_move_constructible_v<decay_t<_Fn>> >>
47 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 auto not_fn(_Fn&& __f) {
48   return __not_fn_t<decay_t<_Fn>>(std::forward<_Fn>(__f));
49 }
50 
51 #endif // _LIBCPP_STD_VER >= 17
52 
53 #if _LIBCPP_STD_VER >= 26
54 
55 template <auto _Fn>
56 struct __nttp_not_fn_t {
57   template <class... _Args>
58   [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Args&&... __args) const
59       noexcept(noexcept(!std::invoke(_Fn, std::forward<_Args>(__args)...)))
60           -> decltype(!std::invoke(_Fn, std::forward<_Args>(__args)...)) {
61     return !std::invoke(_Fn, std::forward<_Args>(__args)...);
62   }
63 };
64 
65 template <auto _Fn>
66 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI constexpr auto not_fn() noexcept {
67   if constexpr (using _Ty = decltype(_Fn); is_pointer_v<_Ty> || is_member_pointer_v<_Ty>)
68     static_assert(_Fn != nullptr, "f cannot be equal to nullptr");
69   return __nttp_not_fn_t<_Fn>();
70 }
71 
72 #endif // _LIBCPP_STD_VER >= 26
73 
74 _LIBCPP_END_NAMESPACE_STD
75 
76 #endif // _LIBCPP___FUNCTIONAL_NOT_FN_H
77