Skip to content

Commit

Permalink
[libc++] Implement not_fn<NTTP>
Browse files Browse the repository at this point in the history
Implement `not_fn<NTTP>` function from P2714R1 Bind front and back to NTTP callables.
  • Loading branch information
JMazurkiewicz committed Mar 21, 2024
1 parent c04807c commit 332c68c
Show file tree
Hide file tree
Showing 13 changed files with 415 additions and 9 deletions.
2 changes: 2 additions & 0 deletions libcxx/docs/FeatureTestMacroTable.rst
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ Status
--------------------------------------------------- -----------------
``__cpp_lib_linalg`` *unimplemented*
--------------------------------------------------- -----------------
``__cpp_lib_not_fn`` ``202306L``
--------------------------------------------------- -----------------
``__cpp_lib_out_ptr`` *unimplemented*
--------------------------------------------------- -----------------
``__cpp_lib_ratio`` ``202306L``
Expand Down
1 change: 1 addition & 0 deletions libcxx/docs/Status/Cxx2c.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Paper Status
.. note::

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

Expand Down
2 changes: 1 addition & 1 deletion libcxx/docs/Status/Cxx2cPapers.csv
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"`P1383R2 <https://wg21.link/P1383R2>`__","LWG","More ``constexpr`` for ``<cmath>`` and ``<complex>``","Varna June 2023","","",""
"`P2734R0 <https://wg21.link/P2734R0>`__","LWG","Adding the new SI prefixes","Varna June 2023","|Complete|","17.0",""
"`P2548R6 <https://wg21.link/P2548R6>`__","LWG","``copyable_function``","Varna June 2023","","",""
"`P2714R1 <https://wg21.link/P2714R1>`__","LWG","Bind front and back to NTTP callables","Varna June 2023","","",""
"`P2714R1 <https://wg21.link/P2714R1>`__","LWG","Bind front and back to NTTP callables","Varna June 2023","|Partial| [#note-P2714R1]_","19.0",""
"`P2630R4 <https://wg21.link/P2630R4>`__","LWG","``submdspan``","Varna June 2023","","",""
"","","","","","",""
"`P0543R3 <https://wg21.link/P0543R3>`__","LWG","Saturation arithmetic","Kona November 2023","|Complete|","18.0",""
Expand Down
23 changes: 23 additions & 0 deletions libcxx/include/__functional/not_fn.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <__type_traits/decay.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_constructible.h>
#include <__type_traits/is_member_pointer.h>
#include <__type_traits/is_pointer.h>
#include <__utility/forward.h>

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

#endif // _LIBCPP_STD_VER >= 17

#if _LIBCPP_STD_VER >= 26

template <auto _Fn>
struct __nttp_not_fn_t {
template <class... _Args>
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Args&&... __args) const
noexcept(noexcept(!std::invoke(_Fn, std::forward<_Args>(__args)...)))
-> decltype(!std::invoke(_Fn, std::forward<_Args>(__args)...)) {
return !std::invoke(_Fn, std::forward<_Args>(__args)...);
}
};

template <auto _Fn>
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto not_fn() noexcept {
if constexpr (using _Ty = decltype(_Fn); is_pointer_v<_Ty> || is_member_pointer_v<_Ty>)
static_assert(_Fn != nullptr, "f cannot be equal to nullptr");
return __nttp_not_fn_t<_Fn>();
}

#endif // _LIBCPP_STD_VER >= 26

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP___FUNCTIONAL_NOT_FN_H
4 changes: 3 additions & 1 deletion libcxx/include/functional
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ template <class Predicate> // deprecated in C++17, removed in C++20
binary_negate<Predicate> not2(const Predicate& pred);
template <class F>
constexpr unspecified not_fn(F&& f); // C++17, constexpr in C++20
constexpr unspecified not_fn(F&& f); // C++17, constexpr in C++20
template <auto f>
constexpr unspecified not_fn() noexcept; // C++26
template<class T> struct is_bind_expression;
template<class T> struct is_placeholder;
Expand Down
5 changes: 4 additions & 1 deletion libcxx/include/version
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ __cpp_lib_nonmember_container_access 201411L <array> <deque>
<iterator> <list> <map>
<regex> <set> <string>
<unordered_map> <unordered_set> <vector>
__cpp_lib_not_fn 201603L <functional>
__cpp_lib_not_fn 202306L <functional>
201603L // C++17
__cpp_lib_null_iterators 201304L <iterator>
__cpp_lib_optional 202110L <optional>
201606L // C++17
Expand Down Expand Up @@ -507,6 +508,8 @@ __cpp_lib_within_lifetime 202306L <type_traits>
// # define __cpp_lib_function_ref 202306L
// # define __cpp_lib_hazard_pointer 202306L
// # define __cpp_lib_linalg 202311L
# undef __cpp_lib_not_fn
# define __cpp_lib_not_fn 202306L
# undef __cpp_lib_out_ptr
// # define __cpp_lib_out_ptr 202311L
# define __cpp_lib_ratio 202306L
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23

// <functional>

// Test implementation-defined properties of std::not_fn<NTTP>.

#include <functional>
#include <type_traits>

struct NotEmptyFunctionObject {
bool val = true;
bool operator()() const; // not defined
};

void test() {
using ResultWithEmptyFuncObject = decltype(std::not_fn<std::false_type{}>());
static_assert(std::is_empty_v<ResultWithEmptyFuncObject>);

using ResultWithNotEmptyFuncObject = decltype(std::not_fn<NotEmptyFunctionObject{}>());
static_assert(std::is_empty_v<ResultWithNotEmptyFuncObject>);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23

// <functional>

// Test the libc++ extension that std::not_fn<NTTP> is marked as [[nodiscard]].

#include <functional>
#include <type_traits>

void test() {
using F = std::true_type;
std::not_fn<F{}>(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
__cpp_lib_invoke_r 202106L [C++23]
__cpp_lib_move_only_function 202110L [C++23]
__cpp_lib_not_fn 201603L [C++17]
202306L [C++26]
__cpp_lib_ranges 202207L [C++20]
__cpp_lib_result_of_sfinae 201210L [C++14]
__cpp_lib_transparent_operators 201210L [C++14]
Expand Down Expand Up @@ -516,8 +517,8 @@
# ifndef __cpp_lib_not_fn
# error "__cpp_lib_not_fn should be defined in c++26"
# endif
# if __cpp_lib_not_fn != 201603L
# error "__cpp_lib_not_fn should have the value 201603L in c++26"
# if __cpp_lib_not_fn != 202306L
# error "__cpp_lib_not_fn should have the value 202306L in c++26"
# endif

# ifndef __cpp_lib_ranges
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
__cpp_lib_node_extract 201606L [C++17]
__cpp_lib_nonmember_container_access 201411L [C++17]
__cpp_lib_not_fn 201603L [C++17]
202306L [C++26]
__cpp_lib_null_iterators 201304L [C++14]
__cpp_lib_optional 201606L [C++17]
202110L [C++23]
Expand Down Expand Up @@ -6968,8 +6969,8 @@
# ifndef __cpp_lib_not_fn
# error "__cpp_lib_not_fn should be defined in c++26"
# endif
# if __cpp_lib_not_fn != 201603L
# error "__cpp_lib_not_fn should have the value 201603L in c++26"
# if __cpp_lib_not_fn != 202306L
# error "__cpp_lib_not_fn should have the value 202306L in c++26"
# endif

# ifndef __cpp_lib_null_iterators
Expand Down
Loading

0 comments on commit 332c68c

Please sign in to comment.