xref: /netbsd-src/external/gpl3/gcc/dist/libstdc++-v3/include/experimental/array (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1f9a78e0eSmrg// <experimental/array> -*- C++ -*-
2f9a78e0eSmrg
3*b1e83836Smrg// Copyright (C) 2015-2022 Free Software Foundation, Inc.
4f9a78e0eSmrg//
5f9a78e0eSmrg// This file is part of the GNU ISO C++ Library.  This library is free
6f9a78e0eSmrg// software; you can redistribute it and/or modify it under the
7f9a78e0eSmrg// terms of the GNU General Public License as published by the
8f9a78e0eSmrg// Free Software Foundation; either version 3, or (at your option)
9f9a78e0eSmrg// any later version.
10f9a78e0eSmrg
11f9a78e0eSmrg// This library is distributed in the hope that it will be useful,
12f9a78e0eSmrg// but WITHOUT ANY WARRANTY; without even the implied warranty of
13f9a78e0eSmrg// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14f9a78e0eSmrg// GNU General Public License for more details.
15f9a78e0eSmrg
16f9a78e0eSmrg// Under Section 7 of GPL version 3, you are granted additional
17f9a78e0eSmrg// permissions described in the GCC Runtime Library Exception, version
18f9a78e0eSmrg// 3.1, as published by the Free Software Foundation.
19f9a78e0eSmrg
20f9a78e0eSmrg// You should have received a copy of the GNU General Public License and
21f9a78e0eSmrg// a copy of the GCC Runtime Library Exception along with this program;
22f9a78e0eSmrg// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23f9a78e0eSmrg// <http://www.gnu.org/licenses/>.
24f9a78e0eSmrg
25f9a78e0eSmrg/** @file experimental/array
26f9a78e0eSmrg *  This is a TS C++ Library header.
27fb8a8121Smrg *  @ingroup libfund-ts
28f9a78e0eSmrg */
29f9a78e0eSmrg
30f9a78e0eSmrg#ifndef _GLIBCXX_EXPERIMENTAL_ARRAY
31f9a78e0eSmrg#define _GLIBCXX_EXPERIMENTAL_ARRAY 1
32f9a78e0eSmrg
33f9a78e0eSmrg#pragma GCC system_header
34f9a78e0eSmrg
35b17d1066Smrg#if __cplusplus >= 201402L
36f9a78e0eSmrg
37f9a78e0eSmrg#include <array>
38f9a78e0eSmrg#include <experimental/type_traits>
39f9a78e0eSmrg
40f9a78e0eSmrgnamespace std _GLIBCXX_VISIBILITY(default)
41f9a78e0eSmrg{
42a3e9eb18Smrg_GLIBCXX_BEGIN_NAMESPACE_VERSION
43a3e9eb18Smrg
44f9a78e0eSmrgnamespace experimental
45f9a78e0eSmrg{
46f9a78e0eSmrginline namespace fundamentals_v2
47f9a78e0eSmrg{
48f9a78e0eSmrg#define __cpp_lib_experimental_make_array 201505
49f9a78e0eSmrg  /**
50f9a78e0eSmrg   * @defgroup make_array Array creation functions
51fb8a8121Smrg   * @ingroup libfund-ts
52f9a78e0eSmrg   *
53f9a78e0eSmrg   * Array creation functions as described in N4529,
54f9a78e0eSmrg   * Working Draft, C++ Extensions for Library Fundamentals, Version 2
55f9a78e0eSmrg   *
56f9a78e0eSmrg   * @{
57f9a78e0eSmrg   */
58f9a78e0eSmrg
59b17d1066Smrgtemplate<typename _Dest, typename... _Types>
60b17d1066Smrg  struct __make_array_elem
61b17d1066Smrg  {
62b17d1066Smrg    using type = _Dest;
63b17d1066Smrg  };
64b17d1066Smrg
65b17d1066Smrgtemplate<typename... _Types>
66b17d1066Smrg  struct __make_array_elem<void, _Types...>
67b17d1066Smrg  : common_type<_Types...>
68b17d1066Smrg  {
69b17d1066Smrg    template <typename>
70f9a78e0eSmrg      struct __is_reference_wrapper : false_type
71f9a78e0eSmrg      {};
72f9a78e0eSmrg
73f9a78e0eSmrg    template <typename _Up>
74f9a78e0eSmrg      struct __is_reference_wrapper<reference_wrapper<_Up>> : true_type
75f9a78e0eSmrg      {};
76f9a78e0eSmrg
77b17d1066Smrg    static_assert(!__or_<__is_reference_wrapper<decay_t<_Types>>...>::value,
78b17d1066Smrg                  "make_array must be used with an explicit target type when"
79b17d1066Smrg                  "any of the arguments is a reference_wrapper");
80b17d1066Smrg  };
81b17d1066Smrg
82fb8a8121Smrg/// Create a std::array from a variable-length list of arguments.
83f9a78e0eSmrgtemplate <typename _Dest = void, typename... _Types>
84b17d1066Smrg  constexpr
85b17d1066Smrg  array<typename __make_array_elem<_Dest, _Types...>::type, sizeof...(_Types)>
86f9a78e0eSmrg  make_array(_Types&&... __t)
87f9a78e0eSmrg  {
88f9a78e0eSmrg    return {{ std::forward<_Types>(__t)... }};
89f9a78e0eSmrg  }
90f9a78e0eSmrg
91f9a78e0eSmrgtemplate <typename _Tp, size_t _Nm, size_t... _Idx>
92f9a78e0eSmrg  constexpr array<remove_cv_t<_Tp>, _Nm>
93f9a78e0eSmrg  __to_array(_Tp (&__a)[_Nm], index_sequence<_Idx...>)
94f9a78e0eSmrg  {
95f9a78e0eSmrg    return {{__a[_Idx]...}};
96f9a78e0eSmrg  }
97f9a78e0eSmrg
98fb8a8121Smrg/// Create a std::array from an array.
99f9a78e0eSmrgtemplate <typename _Tp, size_t _Nm>
100f9a78e0eSmrg  constexpr array<remove_cv_t<_Tp>, _Nm>
101f9a78e0eSmrg  to_array(_Tp (&__a)[_Nm])
102f9a78e0eSmrg  noexcept(is_nothrow_constructible<remove_cv_t<_Tp>, _Tp&>::value)
103f9a78e0eSmrg  {
104fb8a8121Smrg    return experimental::__to_array(__a, make_index_sequence<_Nm>{});
105f9a78e0eSmrg  }
106f9a78e0eSmrg
107a448f87cSmrg  /// @} group make_array
108f9a78e0eSmrg} // namespace fundamentals_v2
109f9a78e0eSmrg} // namespace experimental
110f9a78e0eSmrg
111a3e9eb18Smrg_GLIBCXX_END_NAMESPACE_VERSION
112f9a78e0eSmrg} // namespace std
113f9a78e0eSmrg
114f9a78e0eSmrg#endif // C++14
115f9a78e0eSmrg
116f9a78e0eSmrg#endif // _GLIBCXX_EXPERIMENTAL_ARRAY
117