[libcxx-commits] [libcxx] [libc++][complex] P2819R2: Add `tuple` protocol to `complex` (PR #79744)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Tue Feb 13 01:16:43 PST 2024


================
@@ -1352,6 +1433,67 @@ operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) {
 }
 #endif // !_LIBCPP_HAS_NO_LOCALIZATION
 
+#if _LIBCPP_STD_VER >= 26
+
+// [complex.tuple], tuple interface
+
+template <class _Tp>
+struct tuple_size;
+
+template <class _Tp>
+struct tuple_size<complex<_Tp>> : integral_constant<size_t, 2> {};
+
+template <size_t _Ip, class _Tp>
+struct tuple_element;
+
+template <size_t _Ip, class _Tp>
+struct tuple_element<_Ip, complex<_Tp>> {
+  static_assert(_Ip < 2, "Index value is out of range.");
+  using type = _Tp;
+};
+
+template <size_t _Ip, class _Xp>
+_LIBCPP_HIDE_FROM_ABI constexpr _Xp& get(complex<_Xp>& __z) noexcept {
+  static_assert(_Ip < 2, "Index value is out of range.");
+  if constexpr (_Ip == 0) {
+    return static_cast<_Xp&>(__z.__re_);
+  } else {
+    return static_cast<_Xp&>(__z.__im_);
+  }
+}
+
+template <size_t _Ip, class _Xp>
+_LIBCPP_HIDE_FROM_ABI constexpr _Xp&& get(complex<_Xp>&& __z) noexcept {
+  static_assert(_Ip < 2, "Index value is out of range.");
+  if constexpr (_Ip == 0) {
+    return static_cast<_Xp&&>(__z.__re_);
+  } else {
+    return static_cast<_Xp&&>(__z.__im_);
+  }
----------------
H-G-Hristov wrote:

My excuse here was that after: https://github.com/llvm/llvm-project/commit/7b4622514d232ce5f7110dd8b20d90e81127c467 I couldn't figure out at the time why the CI wouldn't let me use `std::move`. Thank you for reminding me to update this.
I added the necessary: `_LIBCPP_PUSH_MACROS` and `_LIBCPP_POP_MACROS`.

https://github.com/llvm/llvm-project/pull/79744


More information about the libcxx-commits mailing list