[libcxx-commits] [libcxx] [libc++] Simplify the implementation of std::get for pairs (PR #114984)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Nov 5 05:47:59 PST 2024
https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/114984
>From c9a8aa84263137ad922b5dad7d6373ddb708a95a Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Tue, 5 Nov 2024 14:13:03 +0100
Subject: [PATCH] [libc++] Simplify the implementation of std::get for pairs
---
libcxx/include/__utility/pair.h | 16 ++---
.../pairs/pair.astuple/get_ref.pass.cpp | 68 +++++++++++++++++++
2 files changed, 76 insertions(+), 8 deletions(-)
create mode 100644 libcxx/test/std/utilities/utility/pairs/pair.astuple/get_ref.pass.cpp
diff --git a/libcxx/include/__utility/pair.h b/libcxx/include/__utility/pair.h
index f9d0f4e4723113..17631277691108 100644
--- a/libcxx/include/__utility/pair.h
+++ b/libcxx/include/__utility/pair.h
@@ -633,42 +633,42 @@ get(const pair<_T1, _T2>&& __p) _NOEXCEPT {
#if _LIBCPP_STD_VER >= 14
template <class _T1, class _T2>
inline _LIBCPP_HIDE_FROM_ABI constexpr _T1& get(pair<_T1, _T2>& __p) _NOEXCEPT {
- return __get_pair<0>::get(__p);
+ return __p.first;
}
template <class _T1, class _T2>
inline _LIBCPP_HIDE_FROM_ABI constexpr _T1 const& get(pair<_T1, _T2> const& __p) _NOEXCEPT {
- return __get_pair<0>::get(__p);
+ return __p.first;
}
template <class _T1, class _T2>
inline _LIBCPP_HIDE_FROM_ABI constexpr _T1&& get(pair<_T1, _T2>&& __p) _NOEXCEPT {
- return __get_pair<0>::get(std::move(__p));
+ return std::forward<_T1&&>(__p.first);
}
template <class _T1, class _T2>
inline _LIBCPP_HIDE_FROM_ABI constexpr _T1 const&& get(pair<_T1, _T2> const&& __p) _NOEXCEPT {
- return __get_pair<0>::get(std::move(__p));
+ return std::forward<_T1 const&&>(__p.first);
}
template <class _T1, class _T2>
inline _LIBCPP_HIDE_FROM_ABI constexpr _T1& get(pair<_T2, _T1>& __p) _NOEXCEPT {
- return __get_pair<1>::get(__p);
+ return __p.second;
}
template <class _T1, class _T2>
inline _LIBCPP_HIDE_FROM_ABI constexpr _T1 const& get(pair<_T2, _T1> const& __p) _NOEXCEPT {
- return __get_pair<1>::get(__p);
+ return __p.second;
}
template <class _T1, class _T2>
inline _LIBCPP_HIDE_FROM_ABI constexpr _T1&& get(pair<_T2, _T1>&& __p) _NOEXCEPT {
- return __get_pair<1>::get(std::move(__p));
+ return std::forward<_T1&&>(__p.second);
}
template <class _T1, class _T2>
inline _LIBCPP_HIDE_FROM_ABI constexpr _T1 const&& get(pair<_T2, _T1> const&& __p) _NOEXCEPT {
- return __get_pair<1>::get(std::move(__p));
+ return std::forward<_T1 const&&>(__p.second);
}
#endif // _LIBCPP_STD_VER >= 14
diff --git a/libcxx/test/std/utilities/utility/pairs/pair.astuple/get_ref.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pair.astuple/get_ref.pass.cpp
new file mode 100644
index 00000000000000..a363c3ddca7d64
--- /dev/null
+++ b/libcxx/test/std/utilities/utility/pairs/pair.astuple/get_ref.pass.cpp
@@ -0,0 +1,68 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// <utility>
+
+// template <class T1, class T2> struct pair
+
+// template<size_t I, class T1, class T2>
+// typename tuple_element<I, std::pair<T1, T2> >::type&&
+// get(pair<T1, T2>&&);
+
+#include <cassert>
+#include <utility>
+
+#include "test_macros.h"
+
+TEST_CONSTEXPR_CXX14 bool test() {
+ int i = 1;
+ int j = 2;
+
+ {
+ std::pair<int&, int&&> p(i, std::move(j));
+ assert(&std::get<int&>(p) == &i);
+ assert(&std::get<int&&>(p) == &j);
+
+ assert(&std::get<int&>(std::move(p)) == &i);
+ assert(std::get<int&&>(std::move(p)) == 2);
+
+ const std::pair<int&, int&&> cp(i, std::move(j));
+ assert(&std::get<int&>(cp) == &i);
+ assert(&std::get<int&&>(cp) == &j);
+
+ assert(&std::get<int&>(std::move(cp)) == &i);
+ assert(std::get<int&&>(std::move(cp)) == 2);
+ }
+
+ {
+ std::pair<int&&, int&> p(std::move(i), j);
+ assert(&std::get<int&>(p) == &j);
+ assert(&std::get<int&&>(p) == &i);
+
+ assert(&std::get<int&>(std::move(p)) == &j);
+ assert(std::get<int&&>(std::move(p)) == 1);
+
+ const std::pair<int&&, int&> cp(std::move(i), j);
+ assert(&std::get<int&>(cp) == &j);
+ assert(&std::get<int&&>(cp) == &i);
+
+ assert(&std::get<int&>(std::move(cp)) == &j);
+ assert(std::get<int&&>(std::move(cp)) == 1);
+ }
+
+ return true;
+}
+
+int main() {
+ test();
+#if TEST_STD_VER >= 14
+ static_assert(test());
+#endif
+}
More information about the libcxx-commits
mailing list