Skip to content

Commit 332c68c

Browse files
committed
[libc++] Implement not_fn<NTTP>
Implement `not_fn<NTTP>` function from P2714R1 Bind front and back to NTTP callables.
1 parent c04807c commit 332c68c

File tree

13 files changed

+415
-9
lines changed

13 files changed

+415
-9
lines changed

libcxx/docs/FeatureTestMacroTable.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,8 @@ Status
428428
--------------------------------------------------- -----------------
429429
``__cpp_lib_linalg`` *unimplemented*
430430
--------------------------------------------------- -----------------
431+
``__cpp_lib_not_fn`` ``202306L``
432+
--------------------------------------------------- -----------------
431433
``__cpp_lib_out_ptr`` *unimplemented*
432434
--------------------------------------------------- -----------------
433435
``__cpp_lib_ratio`` ``202306L``

libcxx/docs/Status/Cxx2c.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Paper Status
4040
.. note::
4141

4242
.. [#note-P2510R3] This paper is applied as DR against C++20. (MSVC STL and libstdc++ will do the same.)
43+
.. [#note-P2714R1] ``not_fn`` is done.
4344
4445
.. _issues-status-cxx2c:
4546

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>`__","LWG","More ``constexpr`` for ``<cmath>`` and ``<complex>``","Varna June 2023","","",""
2525
"`P2734R0 <https://wg21.link/P2734R0>`__","LWG","Adding the new SI prefixes","Varna June 2023","|Complete|","17.0",""
2626
"`P2548R6 <https://wg21.link/P2548R6>`__","LWG","``copyable_function``","Varna June 2023","","",""
27-
"`P2714R1 <https://wg21.link/P2714R1>`__","LWG","Bind front and back to NTTP callables","Varna June 2023","","",""
27+
"`P2714R1 <https://wg21.link/P2714R1>`__","LWG","Bind front and back to NTTP callables","Varna June 2023","|Partial| [#note-P2714R1]_","19.0",""
2828
"`P2630R4 <https://wg21.link/P2630R4>`__","LWG","``submdspan``","Varna June 2023","","",""
2929
"","","","","","",""
3030
"`P0543R3 <https://wg21.link/P0543R3>`__","LWG","Saturation arithmetic","Kona November 2023","|Complete|","18.0",""

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+
_LIBCPP_NODISCARD_EXT _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+
_LIBCPP_NODISCARD_EXT _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
@@ -205,7 +205,9 @@ template <class Predicate> // deprecated in C++17, removed in C++20
205205
binary_negate<Predicate> not2(const Predicate& pred);
206206
207207
template <class F>
208-
constexpr unspecified not_fn(F&& f); // C++17, constexpr in C++20
208+
constexpr unspecified not_fn(F&& f); // C++17, constexpr in C++20
209+
template <auto f>
210+
constexpr unspecified not_fn() noexcept; // C++26
209211
210212
template<class T> struct is_bind_expression;
211213
template<class T> struct is_placeholder;

libcxx/include/version

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ __cpp_lib_nonmember_container_access 201411L <array> <deque>
154154
<iterator> <list> <map>
155155
<regex> <set> <string>
156156
<unordered_map> <unordered_set> <vector>
157-
__cpp_lib_not_fn 201603L <functional>
157+
__cpp_lib_not_fn 202306L <functional>
158+
201603L // C++17
158159
__cpp_lib_null_iterators 201304L <iterator>
159160
__cpp_lib_optional 202110L <optional>
160161
201606L // C++17
@@ -507,6 +508,8 @@ __cpp_lib_within_lifetime 202306L <type_traits>
507508
// # define __cpp_lib_function_ref 202306L
508509
// # define __cpp_lib_hazard_pointer 202306L
509510
// # define __cpp_lib_linalg 202311L
511+
# undef __cpp_lib_not_fn
512+
# define __cpp_lib_not_fn 202306L
510513
# undef __cpp_lib_out_ptr
511514
// # define __cpp_lib_out_ptr 202311L
512515
# define __cpp_lib_ratio 202306L
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 implementation-defined properties of std::not_fn<NTTP>.
14+
15+
#include <functional>
16+
#include <type_traits>
17+
18+
struct NotEmptyFunctionObject {
19+
bool val = true;
20+
bool operator()() const; // not defined
21+
};
22+
23+
void test() {
24+
using ResultWithEmptyFuncObject = decltype(std::not_fn<std::false_type{}>());
25+
static_assert(std::is_empty_v<ResultWithEmptyFuncObject>);
26+
27+
using ResultWithNotEmptyFuncObject = decltype(std::not_fn<NotEmptyFunctionObject{}>());
28+
static_assert(std::is_empty_v<ResultWithNotEmptyFuncObject>);
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}

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
@@ -28,6 +28,7 @@
2828
__cpp_lib_invoke_r 202106L [C++23]
2929
__cpp_lib_move_only_function 202110L [C++23]
3030
__cpp_lib_not_fn 201603L [C++17]
31+
202306L [C++26]
3132
__cpp_lib_ranges 202207L [C++20]
3233
__cpp_lib_result_of_sfinae 201210L [C++14]
3334
__cpp_lib_transparent_operators 201210L [C++14]
@@ -516,8 +517,8 @@
516517
# ifndef __cpp_lib_not_fn
517518
# error "__cpp_lib_not_fn should be defined in c++26"
518519
# endif
519-
# if __cpp_lib_not_fn != 201603L
520-
# error "__cpp_lib_not_fn should have the value 201603L in c++26"
520+
# if __cpp_lib_not_fn != 202306L
521+
# error "__cpp_lib_not_fn should have the value 202306L in c++26"
521522
# endif
522523

523524
# 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
@@ -144,6 +144,7 @@
144144
__cpp_lib_node_extract 201606L [C++17]
145145
__cpp_lib_nonmember_container_access 201411L [C++17]
146146
__cpp_lib_not_fn 201603L [C++17]
147+
202306L [C++26]
147148
__cpp_lib_null_iterators 201304L [C++14]
148149
__cpp_lib_optional 201606L [C++17]
149150
202110L [C++23]
@@ -6968,8 +6969,8 @@
69686969
# ifndef __cpp_lib_not_fn
69696970
# error "__cpp_lib_not_fn should be defined in c++26"
69706971
# endif
6971-
# if __cpp_lib_not_fn != 201603L
6972-
# error "__cpp_lib_not_fn should have the value 201603L in c++26"
6972+
# if __cpp_lib_not_fn != 202306L
6973+
# error "__cpp_lib_not_fn should have the value 202306L in c++26"
69736974
# endif
69746975

69756976
# ifndef __cpp_lib_null_iterators

0 commit comments

Comments
 (0)