diff mbox series

Define std::to_array for Debug Mode

Message ID 20190926160814.GA29094@redhat.com
State New
Headers show
Series Define std::to_array for Debug Mode | expand

Commit Message

Jonathan Wakely Sept. 26, 2019, 4:08 p.m. UTC
* include/debug/array (to_array): Define for debug mode.

Tested x86_64-linux, committed to trunk.
commit 6404cc56a2e300ffcf5f680f0728e695539a137a
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Sep 26 15:50:30 2019 +0100

    Define std::to_array for Debug Mode
    
            * include/debug/array (to_array): Define for debug mode.
diff mbox series

Patch

diff --git a/libstdc++-v3/include/debug/array b/libstdc++-v3/include/debug/array
index 9e94e656c04..2f8eb842eb8 100644
--- a/libstdc++-v3/include/debug/array
+++ b/libstdc++-v3/include/debug/array
@@ -314,6 +314,45 @@  namespace __debug
       static_assert(_Int < _Nm, "index is out of bounds");
       return std::move(__debug::get<_Int>(__arr));
     }
+
+#if __cplusplus > 201703L
+#define __cpp_lib_to_array 201907L
+
+  template<bool _Move = false, typename _Tp, size_t... _Idx>
+    constexpr array<remove_cv_t<_Tp>, sizeof...(_Idx)>
+    __to_array(_Tp (&__a)[sizeof...(_Idx)], index_sequence<_Idx...>)
+    {
+      if constexpr (_Move)
+	return {{std::move(__a[_Idx])...}};
+      else
+	return {{__a[_Idx]...}};
+    }
+
+  template<typename _Tp, size_t _Nm>
+    constexpr array<remove_cv_t<_Tp>, _Nm>
+    to_array(_Tp (&__a)[_Nm])
+    noexcept(is_nothrow_constructible_v<_Tp, _Tp&>)
+    {
+      static_assert(!is_array_v<_Tp>);
+      static_assert(is_constructible_v<_Tp, _Tp&>);
+      if constexpr (is_constructible_v<_Tp, _Tp&>)
+	return __debug::__to_array(__a, make_index_sequence<_Nm>{});
+      __builtin_unreachable(); // FIXME: see PR c++/91388
+    }
+
+  template<typename _Tp, size_t _Nm>
+    constexpr array<remove_cv_t<_Tp>, _Nm>
+    to_array(_Tp (&&__a)[_Nm])
+    noexcept(is_nothrow_move_constructible_v<_Tp>)
+    {
+      static_assert(!is_array_v<_Tp>);
+      static_assert(is_move_constructible_v<_Tp>);
+      if constexpr (is_move_constructible_v<_Tp>)
+	return __debug::__to_array<1>(__a, make_index_sequence<_Nm>{});
+      __builtin_unreachable(); // FIXME: see PR c++/91388
+    }
+#endif // C++20
+
 } // namespace __debug
 
 _GLIBCXX_BEGIN_NAMESPACE_VERSION