[libcxx-commits] [libcxx] 24c44c3 - [libcxx] adds std::identity to <functional>
Christopher Di Bella via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Mar 29 09:16:21 PDT 2021
Author: Christopher Di Bella
Date: 2021-03-29T16:16:05Z
New Revision: 24c44c379f03227ce45e1d76faacd82fd14f5471
URL: https://github.com/llvm/llvm-project/commit/24c44c379f03227ce45e1d76faacd82fd14f5471
DIFF: https://github.com/llvm/llvm-project/commit/24c44c379f03227ce45e1d76faacd82fd14f5471.diff
LOG: [libcxx] adds std::identity to <functional>
Implements parts of:
- P0898R3 Standard Library Concepts
Differential Revision: https://reviews.llvm.org/D98151
Added:
libcxx/test/std/utilities/function.objects/func.identity/identity.pass.cpp
Modified:
libcxx/include/functional
Removed:
################################################################################
diff --git a/libcxx/include/functional b/libcxx/include/functional
index 8a724f64c9429..e5b85c2bde8df 100644
--- a/libcxx/include/functional
+++ b/libcxx/include/functional
@@ -189,6 +189,8 @@ struct bit_not : unary_function<T, T>
T operator()(const T& x) const;
};
+struct identity; // C++20
+
template <class Predicate>
class unary_negate // deprecated in C++17
: public unary_function<typename Predicate::argument_type, bool>
@@ -3215,6 +3217,19 @@ template <class _Tp>
using unwrap_ref_decay_t = typename unwrap_ref_decay<_Tp>::type;
#endif // > C++17
+#if _LIBCPP_STD_VER > 17
+// [func.identity]
+struct identity {
+ template<class _Tp>
+ constexpr _Tp&& operator()(_Tp&& __t) const noexcept
+ {
+ return _VSTD::forward<_Tp>(__t);
+ }
+
+ using is_transparent = void;
+};
+#endif // _LIBCPP_STD_VER > 17
+
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP_FUNCTIONAL
diff --git a/libcxx/test/std/utilities/function.objects/func.identity/identity.pass.cpp b/libcxx/test/std/utilities/function.objects/func.identity/identity.pass.cpp
new file mode 100644
index 0000000000000..ad1fd9ada8722
--- /dev/null
+++ b/libcxx/test/std/utilities/function.objects/func.identity/identity.pass.cpp
@@ -0,0 +1,77 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+
+// struct identity;
+
+#include <functional>
+
+#include <cassert>
+#include <concepts>
+
+#include "MoveOnly.h"
+
+static_assert(std::semiregular<std::identity>);
+static_assert(requires { typename std::identity::is_transparent; });
+
+constexpr bool test() {
+ std::identity id;
+ int i = 42;
+ assert(id(i) == 42);
+ assert(id(std::move(i)) == 42);
+
+ MoveOnly m1 = 2;
+ MoveOnly m2 = id(std::move(m1));
+ assert(m2.get() == 2);
+
+ assert(&id(i) == &i);
+ static_assert(&id(id) == &id);
+
+ const std::identity idc;
+ assert(idc(1) == 1);
+ assert(std::move(id)(1) == 1);
+ assert(std::move(idc)(1) == 1);
+
+ id = idc; // run-time checks assignment
+ static_assert(std::is_same_v<decltype(id(i)), int&>);
+ static_assert(std::is_same_v<decltype(id(std::declval<int&&>())), int&&>);
+ static_assert(
+ std::is_same_v<decltype(id(std::declval<int const&>())), int const&>);
+ static_assert(
+ std::is_same_v<decltype(id(std::declval<int const&&>())), int const&&>);
+ static_assert(std::is_same_v<decltype(id(std::declval<int volatile&>())),
+ int volatile&>);
+ static_assert(std::is_same_v<decltype(id(std::declval<int volatile&&>())),
+ int volatile&&>);
+ static_assert(
+ std::is_same_v<decltype(id(std::declval<int const volatile&>())),
+ int const volatile&>);
+ static_assert(
+ std::is_same_v<decltype(id(std::declval<int const volatile&&>())),
+ int const volatile&&>);
+
+ struct S {
+ constexpr S() = default;
+ constexpr S(S&&) noexcept(false) {}
+ constexpr S(S const&) noexcept(false) {}
+ };
+ S x;
+ static_assert(noexcept(id(x)));
+ static_assert(noexcept(id(S())));
+
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+
+ return 0;
+}
More information about the libcxx-commits
mailing list