]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
P0325R4 to_array from LFTS with updates
authorJonathan Wakely <jwakely@redhat.com>
Thu, 8 Aug 2019 10:18:53 +0000 (11:18 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Thu, 8 Aug 2019 10:18:53 +0000 (11:18 +0100)
As an extension to what the standard requires, this also adds
conditional noexcept-specifiers to the std::to_array functions.

P0325R4 to_array from LFTS with updates
* include/experimental/array (to_array): Qualify call to __to_array.
* include/std/array (__cpp_lib_to_array, to_array): Define for C++20.
* include/std/version (__cpp_lib_to_array): Likewise.
* testsuite/23_containers/array/creation/1.cc: New test.
* testsuite/23_containers/array/creation/2.cc: New test.
* testsuite/23_containers/array/creation/3_neg.cc: New test.
* testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc:
Use zero for dg-error line number.

From-SVN: r274209

libstdc++-v3/ChangeLog
libstdc++-v3/include/experimental/array
libstdc++-v3/include/std/array
libstdc++-v3/include/std/version
libstdc++-v3/testsuite/23_containers/array/creation/1.cc [new file with mode: 0644]
libstdc++-v3/testsuite/23_containers/array/creation/2.cc [new file with mode: 0644]
libstdc++-v3/testsuite/23_containers/array/creation/3_neg.cc [new file with mode: 0644]
libstdc++-v3/testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc

index c8b9326bdd1d89beb7f36424a61427e68325c04b..29418eb9ad96d07c67b73918b7ac4508402faa66 100644 (file)
@@ -1,3 +1,15 @@
+2019-08-08  Jonathan Wakely  <jwakely@redhat.com>
+
+       P0325R4 to_array from LFTS with updates
+       * include/experimental/array (to_array): Qualify call to __to_array.
+       * include/std/array (__cpp_lib_to_array, to_array): Define for C++20.
+       * include/std/version (__cpp_lib_to_array): Likewise.
+       * testsuite/23_containers/array/creation/1.cc: New test.
+       * testsuite/23_containers/array/creation/2.cc: New test.
+       * testsuite/23_containers/array/creation/3_neg.cc: New test.
+       * testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc:
+       Use zero for dg-error line number.
+
 2019-08-06  Jonathan Wakely  <jwakely@redhat.com>
 
        P1651R0 bind_front should not unwrap reference_wrapper
index 5a35fd499d89916a8282a5d3a499639accb3fa1e..0064a0b3b3daa9d6bcb811e34eb42b7d154a19eb 100644 (file)
@@ -101,7 +101,7 @@ template <typename _Tp, size_t _Nm>
   to_array(_Tp (&__a)[_Nm])
   noexcept(is_nothrow_constructible<remove_cv_t<_Tp>, _Tp&>::value)
   {
-    return __to_array(__a, make_index_sequence<_Nm>{});
+    return experimental::__to_array(__a, make_index_sequence<_Nm>{});
   }
 
   // @} group make_array
index 31eff9e8e1df20d5d35dd9cb1528aa8cbf60ced0..a380f523d440a7d33b576bca51ffaa98f2bb89b6 100644 (file)
@@ -88,7 +88,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
    *  Sets support random access iterators.
    *
    *  @tparam  Tp  Type of element. Required to be a complete type.
-   *  @tparam  N  Number of elements.
+   *  @tparam  Nm  Number of elements.
   */
   template<typename _Tp, std::size_t _Nm>
     struct array
@@ -343,6 +343,44 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       return std::move(_GLIBCXX_STD_C::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 _GLIBCXX_STD_C::__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 _GLIBCXX_STD_C::__to_array<1>(__a, make_index_sequence<_Nm>{});
+      __builtin_unreachable(); // FIXME: see PR c++/91388
+    }
+#endif // C++20
+
 _GLIBCXX_END_NAMESPACE_CONTAINER
 } // namespace std
 
index 9548725d1b7e66abfa2031fd3111a7d18a02cda3..5757568d639248a265486f5182672cb79c3546e3 100644 (file)
 #define __cpp_lib_atomic_ref 201806L
 #define __cpp_lib_bind_front 201907L
 #define __cpp_lib_bounded_array_traits 201902L
+#define __cpp_lib_constexpr_algorithms 201711L
 #if __cpp_impl_destroying_delete
 # define __cpp_lib_destroying_delete 201806L
 #endif
 #endif
 #define __cpp_lib_list_remove_return_type 201806L
 #define __cpp_lib_math_constants 201907L
-#define __cpp_lib_constexpr_algorithms 201711L
+#define __cpp_lib_to_array 201907L
 #endif // C++2a
 #endif // C++17
 #endif // C++14
diff --git a/libstdc++-v3/testsuite/23_containers/array/creation/1.cc b/libstdc++-v3/testsuite/23_containers/array/creation/1.cc
new file mode 100644 (file)
index 0000000..6279d73
--- /dev/null
@@ -0,0 +1,57 @@
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+// Copyright (C) 2019 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <array>
+
+#ifndef __cpp_lib_to_array
+# error "Feature test macro for to_array is missing"
+#elif __cpp_lib_to_array < 201907L
+# error "Feature test macro for to_array has wrong value"
+#endif
+
+void test01()
+{
+  const char x[6]{};
+  std::array<char, 6> y = std::to_array(x);
+
+  constexpr char x2[] = "foo";
+  constexpr std::array<char, 4> y2 = std::to_array(x2);
+  static_assert( std::equal(y2.begin(), y2.end(), x2) );
+}
+
+void
+test02()
+{
+  struct MoveOnly
+  {
+    constexpr MoveOnly(int i = 0) : i(i) { }
+    constexpr MoveOnly(MoveOnly&& m) : i(m.i + 100) { }
+    int i;
+  };
+
+  struct X {
+    MoveOnly m[3];
+  };
+  X x;
+  std::array<MoveOnly, 3> y = std::to_array(std::move(x).m);
+
+  constexpr std::array<MoveOnly, 3> y2 = std::to_array(X{{1, 2, 3}}.m);
+  static_assert( y2[0].i == 101 && y2[1].i == 102 && y2[2].i == 103 );
+}
diff --git a/libstdc++-v3/testsuite/23_containers/array/creation/2.cc b/libstdc++-v3/testsuite/23_containers/array/creation/2.cc
new file mode 100644 (file)
index 0000000..725f950
--- /dev/null
@@ -0,0 +1,27 @@
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+// Copyright (C) 2019 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <version>
+
+#ifndef __cpp_lib_to_array
+# error "Feature test macro for to_array is missing in <version>"
+#elif __cpp_lib_to_array < 201907L
+# error "Feature test macro for to_array has wrong value in <version>"
+#endif
diff --git a/libstdc++-v3/testsuite/23_containers/array/creation/3_neg.cc b/libstdc++-v3/testsuite/23_containers/array/creation/3_neg.cc
new file mode 100644 (file)
index 0000000..72f3899
--- /dev/null
@@ -0,0 +1,56 @@
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+// Copyright (C) 2019 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <array>
+
+void
+test01()
+{
+  int two_dee[3][4];
+  std::to_array(two_dee); // { dg-error "here" }
+}
+
+void
+test02()
+{
+  struct X
+  {
+    int two_dee[3][4];
+  };
+  std::to_array(X{}.two_dee); // { dg-error "here" }
+}
+
+void
+test03()
+{
+  struct MoveOnly
+  {
+    MoveOnly() = default;
+    MoveOnly(MoveOnly&&) = default;
+  };
+
+  MoveOnly mo[2];
+  std::to_array(mo); // { dg-error "here" }
+
+  const MoveOnly cmo[3];
+  std::to_array(std::move(cmo)); // { dg-error "here" }
+}
+
+// { dg-prune-output "static assertion failed" }
index 5de55fc31948e3d5e3fc409f4bf17979d9c7cd99..393f29de5a7c0a64c3a79b622d093b3eb7d73454 100644 (file)
@@ -22,4 +22,4 @@
 
 typedef std::tuple_element<1, std::array<int, 1>>::type type;
 
-// { dg-error "static assertion failed" "" { target *-*-* } 372 }
+// { dg-error "static assertion failed" "" { target *-*-* } 0 }