Skip to content

Commit c91d805

Browse files
[libc++] Implement std::not_fn<NTTP> (#86133)
Implement `std::not_fn<NTTP>` from "P2714R1 Bind front and back to NTTP callables".
1 parent c391082 commit c91d805

File tree

12 files changed

+446
-8
lines changed

12 files changed

+446
-8
lines changed

libcxx/docs/FeatureTestMacroTable.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ Status
458458
---------------------------------------------------------- -----------------
459459
``__cpp_lib_mdspan`` ``202406L``
460460
---------------------------------------------------------- -----------------
461+
``__cpp_lib_not_fn`` ``202306L``
462+
---------------------------------------------------------- -----------------
461463
``__cpp_lib_optional_range_support`` *unimplemented*
462464
---------------------------------------------------------- -----------------
463465
``__cpp_lib_out_ptr`` ``202311L``

libcxx/docs/Status/Cxx2cPapers.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"`P1383R2 <https://wg21.link/P1383R2>`__","More ``constexpr`` for ``<cmath>`` and ``<complex>``","2023-06 (Varna)","","",""
2525
"`P2734R0 <https://wg21.link/P2734R0>`__","Adding the new SI prefixes","2023-06 (Varna)","|Complete|","17",""
2626
"`P2548R6 <https://wg21.link/P2548R6>`__","``copyable_function``","2023-06 (Varna)","","",""
27-
"`P2714R1 <https://wg21.link/P2714R1>`__","Bind front and back to NTTP callables","2023-06 (Varna)","","",""
27+
"`P2714R1 <https://wg21.link/P2714R1>`__","Bind front and back to NTTP callables","2023-06 (Varna)","|Partial|","20","``not_fn`` only"
2828
"`P2630R4 <https://wg21.link/P2630R4>`__","``submdspan``","2023-06 (Varna)","","",""
2929
"","","","","",""
3030
"`P0543R3 <https://wg21.link/P0543R3>`__","Saturation arithmetic","2023-11 (Kona)","|Complete|","18",""

libcxx/include/__functional/not_fn.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <__type_traits/decay.h>
1717
#include <__type_traits/enable_if.h>
1818
#include <__type_traits/is_constructible.h>
19+
#include <__type_traits/is_member_pointer.h>
20+
#include <__type_traits/is_pointer.h>
1921
#include <__utility/forward.h>
2022

2123
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -48,6 +50,27 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 auto not_fn(_Fn&& __f) {
4850

4951
#endif // _LIBCPP_STD_VER >= 17
5052

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+
5174
_LIBCPP_END_NAMESPACE_STD
5275

5376
#endif // _LIBCPP___FUNCTIONAL_NOT_FN_H

libcxx/include/functional

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,9 @@ template <class Predicate> // deprecated in C++17, removed in C++20
214214
binary_negate<Predicate> not2(const Predicate& pred);
215215
216216
template <class F>
217-
constexpr unspecified not_fn(F&& f); // C++17, constexpr in C++20
217+
constexpr unspecified not_fn(F&& f); // C++17, constexpr in C++20
218+
template <auto f>
219+
constexpr unspecified not_fn() noexcept; // C++26
218220
219221
// [func.bind.partial], function templates bind_front and bind_back
220222
template<class F, class... Args>

libcxx/include/version

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ __cpp_lib_nonmember_container_access 201411L <array> <deque>
171171
<iterator> <list> <map>
172172
<regex> <set> <string>
173173
<unordered_map> <unordered_set> <vector>
174-
__cpp_lib_not_fn 201603L <functional>
174+
__cpp_lib_not_fn 202306L <functional>
175+
201603L // C++17
175176
__cpp_lib_null_iterators 201304L <iterator>
176177
__cpp_lib_optional 202110L <optional>
177178
202106L // C++20
@@ -557,6 +558,8 @@ __cpp_lib_void_t 201411L <type_traits>
557558
// # define __cpp_lib_linalg 202311L
558559
# undef __cpp_lib_mdspan
559560
# define __cpp_lib_mdspan 202406L
561+
# undef __cpp_lib_not_fn
562+
# define __cpp_lib_not_fn 202306L
560563
// # define __cpp_lib_optional_range_support 202406L
561564
# undef __cpp_lib_out_ptr
562565
# define __cpp_lib_out_ptr 202311L
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
10+
11+
// <functional>
12+
13+
// Type of `std::not_fn<NTTP>()` is always empty.
14+
15+
#include <functional>
16+
#include <type_traits>
17+
18+
struct NonEmptyFunctionObject {
19+
bool val = true;
20+
bool operator()() const;
21+
};
22+
23+
bool func();
24+
25+
struct SomeClass {
26+
bool member_object;
27+
bool member_function();
28+
};
29+
30+
using ResultWithEmptyFuncObject = decltype(std::not_fn<std::false_type{}>());
31+
static_assert(std::is_empty_v<ResultWithEmptyFuncObject>);
32+
33+
using ResultWithNotEmptyFuncObject = decltype(std::not_fn<NonEmptyFunctionObject{}>());
34+
static_assert(std::is_empty_v<ResultWithNotEmptyFuncObject>);
35+
36+
using ResultWithFunctionPointer = decltype(std::not_fn<&func>());
37+
static_assert(std::is_empty_v<ResultWithFunctionPointer>);
38+
39+
using ResultWithMemberObjectPointer = decltype(std::not_fn<&SomeClass::member_object>());
40+
static_assert(std::is_empty_v<ResultWithMemberObjectPointer>);
41+
42+
using ResultWithMemberFunctionPointer = decltype(std::not_fn<&SomeClass::member_function>());
43+
static_assert(std::is_empty_v<ResultWithMemberFunctionPointer>);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
10+
11+
// <functional>
12+
13+
// Test the libc++ extension that std::not_fn<NTTP> is marked as [[nodiscard]].
14+
15+
#include <functional>
16+
#include <type_traits>
17+
18+
void test() {
19+
using F = std::true_type;
20+
std::not_fn<F{}>(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
21+
22+
auto negated = std::not_fn<F{}>();
23+
negated(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
24+
}

libcxx/test/std/language.support/support.limits/support.limits.general/functional.version.compile.pass.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
__cpp_lib_invoke_r 202106L [C++23]
2828
__cpp_lib_move_only_function 202110L [C++23]
2929
__cpp_lib_not_fn 201603L [C++17]
30+
202306L [C++26]
3031
__cpp_lib_ranges 202110L [C++20]
3132
202406L [C++23]
3233
__cpp_lib_reference_wrapper 202403L [C++26]
@@ -525,8 +526,8 @@
525526
# ifndef __cpp_lib_not_fn
526527
# error "__cpp_lib_not_fn should be defined in c++26"
527528
# endif
528-
# if __cpp_lib_not_fn != 201603L
529-
# error "__cpp_lib_not_fn should have the value 201603L in c++26"
529+
# if __cpp_lib_not_fn != 202306L
530+
# error "__cpp_lib_not_fn should have the value 202306L in c++26"
530531
# endif
531532

532533
# ifndef __cpp_lib_ranges

libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
__cpp_lib_node_extract 201606L [C++17]
157157
__cpp_lib_nonmember_container_access 201411L [C++17]
158158
__cpp_lib_not_fn 201603L [C++17]
159+
202306L [C++26]
159160
__cpp_lib_null_iterators 201304L [C++14]
160161
__cpp_lib_optional 201606L [C++17]
161162
202106L [C++20]
@@ -7405,8 +7406,8 @@
74057406
# ifndef __cpp_lib_not_fn
74067407
# error "__cpp_lib_not_fn should be defined in c++26"
74077408
# endif
7408-
# if __cpp_lib_not_fn != 201603L
7409-
# error "__cpp_lib_not_fn should have the value 201603L in c++26"
7409+
# if __cpp_lib_not_fn != 202306L
7410+
# error "__cpp_lib_not_fn should have the value 202306L in c++26"
74107411
# endif
74117412

74127413
# ifndef __cpp_lib_null_iterators

0 commit comments

Comments
 (0)