array 3.2 KB

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