[libcxx-commits] [libcxx] [libc++] Implement P2819: Add tuple protocol to complex (PR #72776)
via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Nov 20 03:30:16 PST 2023
================
@@ -1538,6 +1549,73 @@ inline namespace literals
} // namespace literals
#endif
+#if _LIBCPP_STD_VER >= 26
+// Tuple interface [complex.tuple]
+template<class _Tp>
+struct _LIBCPP_TEMPLATE_VIS tuple_size<complex<_Tp>>
+ : public integral_constant<size_t, 2> {};
+
+template<size_t _Ip, class _Tp>
+struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, complex<_Tp>>
+{
+ static_assert(_Ip < 2, "Index out of bounds in std::tuple_element<std::complex<T>>");
+
+ using type _LIBCPP_NODEBUG = _Tp;
+};
+
+template <size_t _Ip>
+struct __get_complex
+{
+ static_assert(_Ip < 2, "Index out of bounds in std::get<std::complex<T>>");
+
+ template <typename _Tp>
+ struct to_array_type;
+
+ template <typename _Tp>
+ struct to_array_type<complex<_Tp>>
+ {
+ using type _LIBCPP_NODEBUG = _Tp[2];
+ };
+
+ template <class _Tp>
+ static _LIBCPP_HIDE_FROM_ABI constexpr
+ decltype(auto)
+ get(_Tp&& __z) _NOEXCEPT
+ {
+ using _Array_type = typename __get_complex::to_array_type<std::remove_cvref_t<_Tp>>::type;
+ return reinterpret_cast<_ForwardLike<_Tp, _Array_type>>(__z)[_Ip];
----------------
PragmaTwice wrote:
Good catch! I've got your idea.
Since there are some template specializations of std::complex, maybe friend class is a more easy way.
Thank you!
https://github.com/llvm/llvm-project/pull/72776
More information about the libcxx-commits
mailing list