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

Mark de Wever via libcxx-commits libcxx-commits at lists.llvm.org
Mon Feb 12 11:16:37 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_);
+  }
----------------
mordante wrote:

why did you use your method over?
```suggestion
  if constexpr (_Ip == 0)
    return std::move(__z.__re_);
  else
    return std::move(__z.__im_);
```
The above seems how we typically write this code.

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


More information about the libcxx-commits mailing list