diff mbox

[v3] Implement make_array and to_array from the Fundamentals v2 TS draft

Message ID CAFk2RUajsk2gPCSD0VvAwCjX0QkE6oa+=1RdnVZ_ObFWkUR3Xg@mail.gmail.com
State New
Headers show

Commit Message

Ville Voutilainen July 12, 2015, 6:45 p.m. UTC
Tested on Linux-PPC64.

2015-07-12  Ville Voutilainen  <ville.voutilainen@gmail.com>
    Implement std::experimental::fundamentals_v2::make_array and
    std::experimental::fundamentals_v2::to_array.
    * include/Makefile.am: Add array.
    * include/Makefile.in: Add array.
    * include/experimental/array: New.
    * testsuite/experimental/array/make_array.cc: Likewise.
    * testsuite/experimental/array/neg.cc: Likewise.
diff mbox

Patch

diff --git a/libstdc++-v3/include/Makefile.am b/libstdc++-v3/include/Makefile.am
index 05be8ad..41fc4af 100644
--- a/libstdc++-v3/include/Makefile.am
+++ b/libstdc++-v3/include/Makefile.am
@@ -646,6 +646,7 @@  experimental_builddir = ./experimental
 experimental_headers = \
 	${experimental_srcdir}/algorithm \
 	${experimental_srcdir}/any \
+	${experimental_srcdir}/array \
 	${experimental_srcdir}/chrono \
 	${experimental_srcdir}/deque \
 	${experimental_srcdir}/erase_if.h \
@@ -657,6 +658,7 @@  experimental_headers = \
 	${experimental_srcdir}/memory \
 	${experimental_srcdir}/numeric \
 	${experimental_srcdir}/optional \
+	${experimental_srcdir}/propagate_const \
 	${experimental_srcdir}/ratio \
 	${experimental_srcdir}/set \
 	${experimental_srcdir}/string \
diff --git a/libstdc++-v3/include/Makefile.in b/libstdc++-v3/include/Makefile.in
index bab83b4..b2a140c 100644
--- a/libstdc++-v3/include/Makefile.in
+++ b/libstdc++-v3/include/Makefile.in
@@ -935,6 +935,7 @@  experimental_builddir = ./experimental
 experimental_headers = \
 	${experimental_srcdir}/algorithm \
 	${experimental_srcdir}/any \
+	${experimental_srcdir}/array \
 	${experimental_srcdir}/chrono \
 	${experimental_srcdir}/deque \
 	${experimental_srcdir}/erase_if.h \
@@ -946,6 +947,7 @@  experimental_headers = \
 	${experimental_srcdir}/memory \
 	${experimental_srcdir}/numeric \
 	${experimental_srcdir}/optional \
+	${experimental_srcdir}/propagate_const \
 	${experimental_srcdir}/ratio \
 	${experimental_srcdir}/set \
 	${experimental_srcdir}/string \
diff --git a/libstdc++-v3/include/experimental/array b/libstdc++-v3/include/experimental/array
new file mode 100644
index 0000000..1e1b60e
--- /dev/null
+++ b/libstdc++-v3/include/experimental/array
@@ -0,0 +1,106 @@ 
+// <experimental/array> -*- C++ -*-
+
+// Copyright (C) 2015 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.
+
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
+
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+// <http://www.gnu.org/licenses/>.
+
+/** @file experimental/array
+ *  This is a TS C++ Library header.
+ */
+
+#ifndef _GLIBCXX_EXPERIMENTAL_ARRAY
+#define _GLIBCXX_EXPERIMENTAL_ARRAY 1
+
+#pragma GCC system_header
+
+#if __cplusplus <= 201103L
+# include <bits/c++14_warning.h>
+#else
+
+#include <array>
+#include <functional>
+#include <experimental/type_traits>
+
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+namespace experimental
+{
+inline namespace fundamentals_v2
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+  /**
+   * @defgroup make_array Array creation functions
+   * @ingroup experimental
+   *
+   * Array creation functions as described in N4529,
+   * Working Draft, C++ Extensions for Library Fundamentals, Version 2
+   *
+   * @{
+   */
+
+template <typename _Up>
+struct __is_reference_wrapper : false_type
+{ };
+
+template <typename _Up>
+struct __is_reference_wrapper<reference_wrapper<_Up>> : true_type
+{ };
+
+template <typename _D = void, typename... _Types>
+constexpr auto make_array(_Types&&... __t)
+  -> array<conditional_t<is_same_v<_D, void>,
+                         common_type_t<_Types...>,
+                         _D>,
+           sizeof...(_Types)>
+{
+  static_assert(__or_<__not_<is_same<_D, void>>,
+                __and_<__not_<__is_reference_wrapper<decay_t<_Types>>>...>>
+                ::value,
+                "make_array cannot be used without an explicit target type "
+                "if any of the types given is a reference_wrapper");
+  return {{__t...}};
+}
+
+template <typename _Tp, size_t _N, size_t... _Idx>
+constexpr array<remove_cv_t<_Tp>, _N>
+__to_array(_Tp (&__a)[_N],
+           index_sequence<_Idx...>)
+{
+  return {{__a[_Idx]...}};
+}
+
+template <typename _Tp, size_t _N>
+constexpr array<remove_cv_t<_Tp>, _N> to_array(_Tp (&__a)[_N])
+{
+  return __to_array(__a, make_index_sequence<_N>{});
+}
+
+  // @} group make_array
+  _GLIBCXX_END_NAMESPACE_VERSION
+} // namespace fundamentals_v2
+} // namespace experimental
+
+} // namespace std
+
+#endif // C++14
+
+#endif // _GLIBCXX_EXPERIMENTAL_ARRAY
diff --git a/libstdc++-v3/testsuite/experimental/array/make_array.cc b/libstdc++-v3/testsuite/experimental/array/make_array.cc
new file mode 100644
index 0000000..5a01146
--- /dev/null
+++ b/libstdc++-v3/testsuite/experimental/array/make_array.cc
@@ -0,0 +1,38 @@ 
+// { dg-options "-std=gnu++14" }
+// { dg-do run }
+
+// Copyright (C) 2015 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 moved_to of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <experimental/array>
+
+int main()
+{
+  char x[42];
+  std::array<char, 42> y = std::experimental::to_array(x);
+  std::array<int, 5> z = std::experimental::make_array(1,2,3,4,5);
+  std::array<long, 3> zz = std::experimental::make_array(1,2L, 3);
+  int dummy;
+  auto good = std::experimental::make_array<
+    std::reference_wrapper<int>>(std::ref(dummy));
+  constexpr char x2[42]{};
+  constexpr std::array<char, 42> y2 = std::experimental::to_array(x2);
+  constexpr std::array<int, 5> z2 =
+    std::experimental::make_array(1,2,3,4,5);
+  constexpr std::array<long, 3> zz2
+    = std::experimental::make_array(1,2L, 3);
+}
diff --git a/libstdc++-v3/testsuite/experimental/array/neg.cc b/libstdc++-v3/testsuite/experimental/array/neg.cc
new file mode 100644
index 0000000..643466b
--- /dev/null
+++ b/libstdc++-v3/testsuite/experimental/array/neg.cc
@@ -0,0 +1,28 @@ 
+// { dg-options "-std=gnu++14" }
+// { dg-do compile }
+
+// Copyright (C) 2015 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 moved_to of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <experimental/array>
+
+int main()
+{
+  int dummy;
+  auto bad = std::experimental::make_array(std::ref(dummy));
+  // { dg-error "make_array cannot be used without an explicit target type if any of the types given is a reference_wrapper" "" { target *-*-* } 75 }
+}