[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
Thu Mar 27 06:56:51 PDT 2025


https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/114984

>From 331ac8c12cd149175526aa9574507deda9318268 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/pairs.by.type.pass.cpp | 105 ++++++++++++------
 2 files changed, 82 insertions(+), 39 deletions(-)

diff --git a/libcxx/include/__utility/pair.h b/libcxx/include/__utility/pair.h
index 1f596a87f7cc7..3cc22555d4224 100644
--- a/libcxx/include/__utility/pair.h
+++ b/libcxx/include/__utility/pair.h
@@ -629,42 +629,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/pairs.by.type.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pair.astuple/pairs.by.type.pass.cpp
index f6940c468a81d..588bb7a033900 100644
--- a/libcxx/test/std/utilities/utility/pairs/pair.astuple/pairs.by.type.pass.cpp
+++ b/libcxx/test/std/utilities/utility/pairs/pair.astuple/pairs.by.type.pass.cpp
@@ -8,43 +8,77 @@
 
 // UNSUPPORTED: c++03, c++11
 
-#include <utility>
-#include <string>
-#include <type_traits>
+#include <cassert>
 #include <complex>
 #include <memory>
-
-#include <cassert>
+#include <type_traits>
+#include <utility>
 
 #include "test_macros.h"
 
-int main(int, char**)
-{
-    typedef std::complex<float> cf;
+TEST_CONSTEXPR_CXX14 bool test() {
+  { // Make sure that references work as expected
+    int i = 1;
+    int j = 2;
+
     {
-    auto t1 = std::make_pair<int, cf> ( 42, { 1,2 } );
-    assert ( std::get<int>(t1) == 42 );
-    assert ( std::get<cf>(t1).real() == 1 );
-    assert ( std::get<cf>(t1).imag() == 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);
     }
 
     {
-    const std::pair<int, const int> p1 { 1, 2 };
-    const int &i1 = std::get<int>(p1);
-    const int &i2 = std::get<const int>(p1);
-    assert ( i1 == 1 );
-    assert ( i2 == 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);
     }
+  }
 
-    {
+  {
+    typedef std::complex<float> cf;
+    auto t1  = std::make_pair<int, cf>(42, {1, 2});
+    assert(std::get<int>(t1) == 42);
+    assert(std::get<cf>(t1).real() == 1);
+    assert(std::get<cf>(t1).imag() == 2);
+  }
+
+  {
+    const std::pair<int, const int> p1{1, 2};
+    const int& i1 = std::get<int>(p1);
+    const int& i2 = std::get<const int>(p1);
+    assert(i1 == 1);
+    assert(i2 == 2);
+  }
+
+  {
     typedef std::unique_ptr<int> upint;
     std::pair<upint, int> t(upint(new int(4)), 42);
     upint p = std::get<upint>(std::move(t)); // get rvalue
     assert(*p == 4);
     assert(std::get<upint>(t) == nullptr); // has been moved from
-    }
+  }
 
-    {
+  {
     typedef std::unique_ptr<int> upint;
     const std::pair<upint, int> t(upint(new int(4)), 42);
     static_assert(std::is_same<const upint&&, decltype(std::get<upint>(std::move(t)))>::value, "");
@@ -52,37 +86,46 @@ int main(int, char**)
     static_assert(std::is_same<const int&&, decltype(std::get<int>(std::move(t)))>::value, "");
     static_assert(noexcept(std::get<int>(std::move(t))), "");
     auto&& p = std::get<upint>(std::move(t)); // get const rvalue
-    auto&& i = std::get<int>(std::move(t)); // get const rvalue
+    auto&& i = std::get<int>(std::move(t));   // get const rvalue
     assert(*p == 4);
     assert(i == 42);
     assert(std::get<upint>(t) != nullptr);
-    }
+  }
 
-    {
-    int x = 42;
+  {
+    int x       = 42;
     int const y = 43;
     std::pair<int&, int const&> const p(x, y);
     static_assert(std::is_same<int&, decltype(std::get<int&>(std::move(p)))>::value, "");
     static_assert(noexcept(std::get<int&>(std::move(p))), "");
     static_assert(std::is_same<int const&, decltype(std::get<int const&>(std::move(p)))>::value, "");
     static_assert(noexcept(std::get<int const&>(std::move(p))), "");
-    }
+  }
 
-    {
-    int x = 42;
+  {
+    int x       = 42;
     int const y = 43;
     std::pair<int&&, int const&&> const p(std::move(x), std::move(y));
     static_assert(std::is_same<int&&, decltype(std::get<int&&>(std::move(p)))>::value, "");
     static_assert(noexcept(std::get<int&&>(std::move(p))), "");
     static_assert(std::is_same<int const&&, decltype(std::get<int const&&>(std::move(p)))>::value, "");
     static_assert(noexcept(std::get<int const&&>(std::move(p))), "");
-    }
+  }
 
-    {
-    constexpr const std::pair<int, const int> p { 1, 2 };
+  {
+    constexpr const std::pair<int, const int> p{1, 2};
     static_assert(std::get<int>(std::move(p)) == 1, "");
     static_assert(std::get<const int>(std::move(p)) == 2, "");
-    }
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+#if TEST_STD_VER >= 14
+  static_assert(test(), "");
+#endif
 
   return 0;
 }



More information about the libcxx-commits mailing list