Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===---------------------------- array -----------------------------------===// |
| 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_ARRAY |
| 11 | #define _LIBCPP_ARRAY |
| 12 | |
| 13 | /* |
| 14 | array synopsis |
| 15 | |
| 16 | namespace std |
| 17 | { |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 18 | template <class T, size_t N > |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 19 | struct array |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 20 | { |
| 21 | // types: |
| 22 | typedef T & reference; |
| 23 | typedef const T & const_reference; |
| 24 | typedef implementation defined iterator; |
| 25 | typedef implementation defined const_iterator; |
| 26 | typedef size_t size_type; |
| 27 | typedef ptrdiff_t difference_type; |
| 28 | typedef T value_type; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 29 | typedef T* pointer; |
| 30 | typedef const T* const_pointer; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 31 | typedef std::reverse_iterator<iterator> reverse_iterator; |
| 32 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 33 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 34 | // No explicit construct/copy/destroy for aggregate type |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 35 | void fill(const T& u); // constexpr in C++20 |
| 36 | void swap(array& a) noexcept(is_nothrow_swappable_v<T>); // constexpr in C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 37 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 38 | // iterators: |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 39 | iterator begin() noexcept; // constexpr in C++17 |
| 40 | const_iterator begin() const noexcept; // constexpr in C++17 |
| 41 | iterator end() noexcept; // constexpr in C++17 |
| 42 | const_iterator end() const noexcept; // constexpr in C++17 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 43 | |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 44 | reverse_iterator rbegin() noexcept; // constexpr in C++17 |
| 45 | const_reverse_iterator rbegin() const noexcept; // constexpr in C++17 |
| 46 | reverse_iterator rend() noexcept; // constexpr in C++17 |
| 47 | const_reverse_iterator rend() const noexcept; // constexpr in C++17 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 48 | |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 49 | const_iterator cbegin() const noexcept; // constexpr in C++17 |
| 50 | const_iterator cend() const noexcept; // constexpr in C++17 |
| 51 | const_reverse_iterator crbegin() const noexcept; // constexpr in C++17 |
| 52 | const_reverse_iterator crend() const noexcept; // constexpr in C++17 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 53 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 54 | // capacity: |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 55 | constexpr size_type size() const noexcept; |
| 56 | constexpr size_type max_size() const noexcept; |
Howard Hinnant | 12ff42f | 2012-07-20 19:20:49 +0000 | [diff] [blame] | 57 | constexpr bool empty() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 58 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 59 | // element access: |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 60 | reference operator[](size_type n); // constexpr in C++17 |
| 61 | const_reference operator[](size_type n) const; // constexpr in C++14 |
| 62 | reference at(size_type n); // constexpr in C++17 |
| 63 | const_reference at(size_type n) const; // constexpr in C++14 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 64 | |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 65 | reference front(); // constexpr in C++17 |
| 66 | const_reference front() const; // constexpr in C++14 |
| 67 | reference back(); // constexpr in C++17 |
| 68 | const_reference back() const; // constexpr in C++14 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 69 | |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 70 | T* data() noexcept; // constexpr in C++17 |
| 71 | const T* data() const noexcept; // constexpr in C++17 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 74 | template <class T, class... U> |
| 75 | array(T, U...) -> array<T, 1 + sizeof...(U)>; // C++17 |
Marshall Clow | 8847d03 | 2018-05-18 21:01:04 +0000 | [diff] [blame] | 76 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 77 | template <class T, size_t N> |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 78 | bool operator==(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20 |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 79 | template <class T, size_t N> |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 80 | bool operator!=(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20 |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 81 | template <class T, size_t N> |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 82 | bool operator<(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20 |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 83 | template <class T, size_t N> |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 84 | bool operator>(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20 |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 85 | template <class T, size_t N> |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 86 | bool operator<=(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20 |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 87 | template <class T, size_t N> |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 88 | bool operator>=(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 89 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 90 | template <class T, size_t N > |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 91 | void swap(array<T,N>& x, array<T,N>& y) noexcept(noexcept(x.swap(y))); // constexpr in C++20 |
| 92 | |
| 93 | template <class T, size_t N> |
| 94 | constexpr array<remove_cv_t<T>, N> to_array(T (&a)[N]); // C++20 |
| 95 | template <class T, size_t N> |
| 96 | constexpr array<remove_cv_t<T>, N> to_array(T (&&a)[N]); // C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 97 | |
Marshall Clow | ce62b4d | 2019-01-11 21:57:12 +0000 | [diff] [blame] | 98 | template <class T> struct tuple_size; |
Louis Dionne | e684aa6 | 2019-04-01 16:39:34 +0000 | [diff] [blame] | 99 | template <size_t I, class T> struct tuple_element; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 100 | template <class T, size_t N> struct tuple_size<array<T, N>>; |
Marshall Clow | 291a351 | 2015-11-19 19:41:04 +0000 | [diff] [blame] | 101 | template <size_t I, class T, size_t N> struct tuple_element<I, array<T, N>>; |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 102 | template <size_t I, class T, size_t N> T& get(array<T, N>&) noexcept; // constexpr in C++14 |
| 103 | template <size_t I, class T, size_t N> const T& get(const array<T, N>&) noexcept; // constexpr in C++14 |
| 104 | template <size_t I, class T, size_t N> T&& get(array<T, N>&&) noexcept; // constexpr in C++14 |
Eric Fiselier | 6dea809 | 2015-12-18 00:36:55 +0000 | [diff] [blame] | 105 | template <size_t I, class T, size_t N> const T&& get(const array<T, N>&&) noexcept; // constexpr in C++14 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 106 | |
| 107 | } // std |
| 108 | |
| 109 | */ |
| 110 | |
| 111 | #include <__config> |
| 112 | #include <__tuple> |
| 113 | #include <type_traits> |
| 114 | #include <utility> |
| 115 | #include <iterator> |
| 116 | #include <algorithm> |
| 117 | #include <stdexcept> |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 118 | #include <cstdlib> // for _LIBCPP_UNREACHABLE |
Marshall Clow | 0a1e750 | 2018-09-12 19:41:40 +0000 | [diff] [blame] | 119 | #include <version> |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 120 | #include <__debug> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 121 | |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 122 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 123 | #pragma GCC system_header |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 124 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 125 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 126 | |
| 127 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 128 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 129 | |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 130 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 131 | template <class _Tp, size_t _Size> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 132 | struct _LIBCPP_TEMPLATE_VIS array |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 133 | { |
| 134 | // types: |
| 135 | typedef array __self; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 136 | typedef _Tp value_type; |
| 137 | typedef value_type& reference; |
| 138 | typedef const value_type& const_reference; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 139 | typedef value_type* iterator; |
| 140 | typedef const value_type* const_iterator; |
| 141 | typedef value_type* pointer; |
| 142 | typedef const value_type* const_pointer; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 143 | typedef size_t size_type; |
| 144 | typedef ptrdiff_t difference_type; |
| 145 | typedef std::reverse_iterator<iterator> reverse_iterator; |
| 146 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 147 | |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 148 | _Tp __elems_[_Size]; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 149 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 150 | // No explicit construct/copy/destroy for aggregate type |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 151 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
| 152 | void fill(const value_type& __u) { |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 153 | _VSTD::fill_n(__elems_, _Size, __u); |
| 154 | } |
Eric Fiselier | 55659e4 | 2018-02-04 01:03:08 +0000 | [diff] [blame] | 155 | |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 156 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 157 | void swap(array& __a) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) { |
| 158 | std::swap_ranges(__elems_, __elems_ + _Size, __a.__elems_); |
| 159 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 160 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 161 | // iterators: |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 162 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 163 | iterator begin() _NOEXCEPT {return iterator(data());} |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 164 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 165 | const_iterator begin() const _NOEXCEPT {return const_iterator(data());} |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 166 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 167 | iterator end() _NOEXCEPT {return iterator(data() + _Size);} |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 168 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 169 | const_iterator end() const _NOEXCEPT {return const_iterator(data() + _Size);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 170 | |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 171 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 172 | reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());} |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 173 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 174 | const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());} |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 175 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 176 | reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());} |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 177 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 178 | const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 179 | |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 180 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 181 | const_iterator cbegin() const _NOEXCEPT {return begin();} |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 182 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 183 | const_iterator cend() const _NOEXCEPT {return end();} |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 184 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 185 | const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();} |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 186 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 187 | const_reverse_iterator crend() const _NOEXCEPT {return rend();} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 188 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 189 | // capacity: |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 190 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 12ff42f | 2012-07-20 19:20:49 +0000 | [diff] [blame] | 191 | _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return _Size;} |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 192 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 12ff42f | 2012-07-20 19:20:49 +0000 | [diff] [blame] | 193 | _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return _Size;} |
Marshall Clow | 425f575 | 2017-11-15 05:51:26 +0000 | [diff] [blame] | 194 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 195 | _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return false; } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 196 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 197 | // element access: |
Marshall Clow | ac63f41 | 2017-01-16 03:02:10 +0000 | [diff] [blame] | 198 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Marshall Clow | 8d7c9f1 | 2019-03-14 21:56:57 +0000 | [diff] [blame] | 199 | reference operator[](size_type __n) _NOEXCEPT {return __elems_[__n];} |
Marshall Clow | ac63f41 | 2017-01-16 03:02:10 +0000 | [diff] [blame] | 200 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Marshall Clow | 8d7c9f1 | 2019-03-14 21:56:57 +0000 | [diff] [blame] | 201 | const_reference operator[](size_type __n) const _NOEXCEPT {return __elems_[__n];} |
Marshall Clow | ac63f41 | 2017-01-16 03:02:10 +0000 | [diff] [blame] | 202 | |
Louis Dionne | 8468f97 | 2020-05-22 09:23:32 -0400 | [diff] [blame] | 203 | _LIBCPP_CONSTEXPR_AFTER_CXX14 reference at(size_type __n) |
| 204 | { |
| 205 | if (__n >= _Size) |
| 206 | __throw_out_of_range("array::at"); |
| 207 | return __elems_[__n]; |
| 208 | } |
| 209 | |
| 210 | _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference at(size_type __n) const |
| 211 | { |
| 212 | if (__n >= _Size) |
| 213 | __throw_out_of_range("array::at"); |
| 214 | return __elems_[__n]; |
| 215 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 216 | |
Marshall Clow | 05cf669 | 2019-03-19 03:30:07 +0000 | [diff] [blame] | 217 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference front() _NOEXCEPT {return __elems_[0];} |
| 218 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference front() const _NOEXCEPT {return __elems_[0];} |
| 219 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference back() _NOEXCEPT {return __elems_[_Size - 1];} |
| 220 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference back() const _NOEXCEPT {return __elems_[_Size - 1];} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 221 | |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 222 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Nirav Dave | 129fb5c | 2018-02-06 03:03:37 +0000 | [diff] [blame] | 223 | value_type* data() _NOEXCEPT {return __elems_;} |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 224 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Nirav Dave | 129fb5c | 2018-02-06 03:03:37 +0000 | [diff] [blame] | 225 | const value_type* data() const _NOEXCEPT {return __elems_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 228 | template <class _Tp> |
| 229 | struct _LIBCPP_TEMPLATE_VIS array<_Tp, 0> |
| 230 | { |
| 231 | // types: |
| 232 | typedef array __self; |
| 233 | typedef _Tp value_type; |
| 234 | typedef value_type& reference; |
| 235 | typedef const value_type& const_reference; |
| 236 | typedef value_type* iterator; |
| 237 | typedef const value_type* const_iterator; |
| 238 | typedef value_type* pointer; |
| 239 | typedef const value_type* const_pointer; |
| 240 | typedef size_t size_type; |
| 241 | typedef ptrdiff_t difference_type; |
| 242 | typedef std::reverse_iterator<iterator> reverse_iterator; |
| 243 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
| 244 | |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 245 | #ifndef _LIBCPP_CXX03_LANG |
| 246 | union __wrapper { |
| 247 | _LIBCPP_CONSTEXPR __wrapper() : __b() { } |
| 248 | ~__wrapper() = default; |
| 249 | |
| 250 | bool __b; |
| 251 | _Tp __t; |
| 252 | } __w; |
| 253 | |
| 254 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 255 | value_type* data() _NOEXCEPT {return &__w.__t;} |
| 256 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 257 | const value_type* data() const _NOEXCEPT {return &__w.__t;} |
| 258 | #else // C++03 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 259 | typedef typename conditional<is_const<_Tp>::value, const char, |
| 260 | char>::type _CharType; |
Eric Fiselier | 6a53758 | 2018-02-07 23:50:25 +0000 | [diff] [blame] | 261 | |
| 262 | struct _ArrayInStructT { _Tp __data_[1]; }; |
| 263 | _ALIGNAS_TYPE(_ArrayInStructT) _CharType __elems_[sizeof(_ArrayInStructT)]; |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 264 | |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 265 | _LIBCPP_INLINE_VISIBILITY |
| 266 | value_type* data() _NOEXCEPT {return reinterpret_cast<value_type*>(__elems_);} |
| 267 | _LIBCPP_INLINE_VISIBILITY |
| 268 | const value_type* data() const _NOEXCEPT {return reinterpret_cast<const value_type*>(__elems_);} |
| 269 | #endif |
| 270 | |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 271 | // No explicit construct/copy/destroy for aggregate type |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 272 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
| 273 | void fill(const value_type&) { |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 274 | static_assert(!is_const<_Tp>::value, |
| 275 | "cannot fill zero-sized array of type 'const T'"); |
| 276 | } |
| 277 | |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 278 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 279 | void swap(array&) _NOEXCEPT { |
| 280 | static_assert(!is_const<_Tp>::value, |
| 281 | "cannot swap zero-sized array of type 'const T'"); |
| 282 | } |
| 283 | |
| 284 | // iterators: |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 285 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 286 | iterator begin() _NOEXCEPT {return iterator(data());} |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 287 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 288 | const_iterator begin() const _NOEXCEPT {return const_iterator(data());} |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 289 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 290 | iterator end() _NOEXCEPT {return iterator(data());} |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 291 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 292 | const_iterator end() const _NOEXCEPT {return const_iterator(data());} |
| 293 | |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 294 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 295 | reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());} |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 296 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 297 | const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());} |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 298 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 299 | reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());} |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 300 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 301 | const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());} |
| 302 | |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 303 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 304 | const_iterator cbegin() const _NOEXCEPT {return begin();} |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 305 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 306 | const_iterator cend() const _NOEXCEPT {return end();} |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 307 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 308 | const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();} |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 309 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 310 | const_reverse_iterator crend() const _NOEXCEPT {return rend();} |
| 311 | |
| 312 | // capacity: |
| 313 | _LIBCPP_INLINE_VISIBILITY |
| 314 | _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return 0; } |
| 315 | _LIBCPP_INLINE_VISIBILITY |
| 316 | _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return 0;} |
| 317 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY |
| 318 | _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return true;} |
| 319 | |
| 320 | // element access: |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 321 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Marshall Clow | 8d7c9f1 | 2019-03-14 21:56:57 +0000 | [diff] [blame] | 322 | reference operator[](size_type) _NOEXCEPT { |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 323 | _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array"); |
| 324 | _LIBCPP_UNREACHABLE(); |
| 325 | } |
| 326 | |
| 327 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Marshall Clow | 8d7c9f1 | 2019-03-14 21:56:57 +0000 | [diff] [blame] | 328 | const_reference operator[](size_type) const _NOEXCEPT { |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 329 | _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array"); |
| 330 | _LIBCPP_UNREACHABLE(); |
| 331 | } |
| 332 | |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 333 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 334 | reference at(size_type) { |
| 335 | __throw_out_of_range("array<T, 0>::at"); |
| 336 | _LIBCPP_UNREACHABLE(); |
| 337 | } |
| 338 | |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 339 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 340 | const_reference at(size_type) const { |
| 341 | __throw_out_of_range("array<T, 0>::at"); |
| 342 | _LIBCPP_UNREACHABLE(); |
| 343 | } |
| 344 | |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 345 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Marshall Clow | 05cf669 | 2019-03-19 03:30:07 +0000 | [diff] [blame] | 346 | reference front() _NOEXCEPT { |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 347 | _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array"); |
| 348 | _LIBCPP_UNREACHABLE(); |
| 349 | } |
| 350 | |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 351 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Marshall Clow | 05cf669 | 2019-03-19 03:30:07 +0000 | [diff] [blame] | 352 | const_reference front() const _NOEXCEPT { |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 353 | _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array"); |
| 354 | _LIBCPP_UNREACHABLE(); |
| 355 | } |
| 356 | |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 357 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Marshall Clow | 05cf669 | 2019-03-19 03:30:07 +0000 | [diff] [blame] | 358 | reference back() _NOEXCEPT { |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 359 | _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array"); |
| 360 | _LIBCPP_UNREACHABLE(); |
| 361 | } |
| 362 | |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 363 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Marshall Clow | 05cf669 | 2019-03-19 03:30:07 +0000 | [diff] [blame] | 364 | const_reference back() const _NOEXCEPT { |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 365 | _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array"); |
| 366 | _LIBCPP_UNREACHABLE(); |
| 367 | } |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 368 | }; |
| 369 | |
| 370 | |
Marshall Clow | 8847d03 | 2018-05-18 21:01:04 +0000 | [diff] [blame] | 371 | #ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES |
| 372 | template<class _Tp, class... _Args, |
Eric Fiselier | d8ec890 | 2020-04-09 17:39:40 -0400 | [diff] [blame] | 373 | class = _EnableIf<__all<_IsSame<_Tp, _Args>::value...>::value> |
Marshall Clow | 8847d03 | 2018-05-18 21:01:04 +0000 | [diff] [blame] | 374 | > |
| 375 | array(_Tp, _Args...) |
| 376 | -> array<_Tp, 1 + sizeof...(_Args)>; |
| 377 | #endif |
| 378 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 379 | template <class _Tp, size_t _Size> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 380 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 35a4504 | 2018-08-02 02:11:06 +0000 | [diff] [blame] | 381 | _LIBCPP_CONSTEXPR_AFTER_CXX17 bool |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 382 | operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) |
| 383 | { |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 384 | return _VSTD::equal(__x.begin(), __x.end(), __y.begin()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 387 | template <class _Tp, size_t _Size> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 388 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 35a4504 | 2018-08-02 02:11:06 +0000 | [diff] [blame] | 389 | _LIBCPP_CONSTEXPR_AFTER_CXX17 bool |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 390 | operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) |
| 391 | { |
| 392 | return !(__x == __y); |
| 393 | } |
| 394 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 395 | template <class _Tp, size_t _Size> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 396 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 35a4504 | 2018-08-02 02:11:06 +0000 | [diff] [blame] | 397 | _LIBCPP_CONSTEXPR_AFTER_CXX17 bool |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 398 | operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) |
| 399 | { |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 400 | return _VSTD::lexicographical_compare(__x.begin(), __x.end(), |
| 401 | __y.begin(), __y.end()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 404 | template <class _Tp, size_t _Size> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 405 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 35a4504 | 2018-08-02 02:11:06 +0000 | [diff] [blame] | 406 | _LIBCPP_CONSTEXPR_AFTER_CXX17 bool |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 407 | operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) |
| 408 | { |
| 409 | return __y < __x; |
| 410 | } |
| 411 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 412 | template <class _Tp, size_t _Size> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 413 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 35a4504 | 2018-08-02 02:11:06 +0000 | [diff] [blame] | 414 | _LIBCPP_CONSTEXPR_AFTER_CXX17 bool |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 415 | operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) |
| 416 | { |
| 417 | return !(__y < __x); |
| 418 | } |
| 419 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 420 | template <class _Tp, size_t _Size> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 421 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 35a4504 | 2018-08-02 02:11:06 +0000 | [diff] [blame] | 422 | _LIBCPP_CONSTEXPR_AFTER_CXX17 bool |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 423 | operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) |
| 424 | { |
| 425 | return !(__x < __y); |
| 426 | } |
| 427 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 428 | template <class _Tp, size_t _Size> |
Louis Dionne | 44704ce | 2020-05-22 09:59:48 -0400 | [diff] [blame^] | 429 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Howard Hinnant | 4426724 | 2011-06-01 19:59:32 +0000 | [diff] [blame] | 430 | typename enable_if |
| 431 | < |
Eric Fiselier | 6bfed25 | 2016-04-21 23:38:59 +0000 | [diff] [blame] | 432 | _Size == 0 || |
Howard Hinnant | 4426724 | 2011-06-01 19:59:32 +0000 | [diff] [blame] | 433 | __is_swappable<_Tp>::value, |
| 434 | void |
| 435 | >::type |
Marshall Clow | ff1aa3d | 2016-03-07 21:57:10 +0000 | [diff] [blame] | 436 | swap(array<_Tp, _Size>& __x, array<_Tp, _Size>& __y) |
Eric Fiselier | 6bfed25 | 2016-04-21 23:38:59 +0000 | [diff] [blame] | 437 | _NOEXCEPT_(noexcept(__x.swap(__y))) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 438 | { |
| 439 | __x.swap(__y); |
| 440 | } |
| 441 | |
| 442 | template <class _Tp, size_t _Size> |
Marshall Clow | ce62b4d | 2019-01-11 21:57:12 +0000 | [diff] [blame] | 443 | struct _LIBCPP_TEMPLATE_VIS tuple_size<array<_Tp, _Size> > |
Howard Hinnant | 8e29cda | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 444 | : public integral_constant<size_t, _Size> {}; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 445 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 446 | template <size_t _Ip, class _Tp, size_t _Size> |
Louis Dionne | e684aa6 | 2019-04-01 16:39:34 +0000 | [diff] [blame] | 447 | struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, array<_Tp, _Size> > |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 448 | { |
Marshall Clow | 6f77fa1 | 2017-06-12 14:41:37 +0000 | [diff] [blame] | 449 | static_assert(_Ip < _Size, "Index out of bounds in std::tuple_element<> (std::array)"); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 450 | typedef _Tp type; |
| 451 | }; |
| 452 | |
| 453 | template <size_t _Ip, class _Tp, size_t _Size> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 454 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 455 | _Tp& |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 456 | get(array<_Tp, _Size>& __a) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 457 | { |
Marshall Clow | 66c49ca | 2012-12-18 16:46:30 +0000 | [diff] [blame] | 458 | static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)"); |
Marshall Clow | 0abb104 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 459 | return __a.__elems_[_Ip]; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | template <size_t _Ip, class _Tp, size_t _Size> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 463 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 464 | const _Tp& |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 465 | get(const array<_Tp, _Size>& __a) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 466 | { |
Marshall Clow | 66c49ca | 2012-12-18 16:46:30 +0000 | [diff] [blame] | 467 | static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)"); |
Marshall Clow | 0abb104 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 468 | return __a.__elems_[_Ip]; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Eric Fiselier | 73abed7 | 2017-04-16 02:50:40 +0000 | [diff] [blame] | 471 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 22e9724 | 2010-11-17 19:52:17 +0000 | [diff] [blame] | 472 | |
| 473 | template <size_t _Ip, class _Tp, size_t _Size> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 474 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | 22e9724 | 2010-11-17 19:52:17 +0000 | [diff] [blame] | 475 | _Tp&& |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 476 | get(array<_Tp, _Size>&& __a) _NOEXCEPT |
Howard Hinnant | 22e9724 | 2010-11-17 19:52:17 +0000 | [diff] [blame] | 477 | { |
Marshall Clow | 66c49ca | 2012-12-18 16:46:30 +0000 | [diff] [blame] | 478 | static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)"); |
Marshall Clow | 0abb104 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 479 | return _VSTD::move(__a.__elems_[_Ip]); |
Howard Hinnant | 22e9724 | 2010-11-17 19:52:17 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Eric Fiselier | 6dea809 | 2015-12-18 00:36:55 +0000 | [diff] [blame] | 482 | template <size_t _Ip, class _Tp, size_t _Size> |
| 483 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 484 | const _Tp&& |
| 485 | get(const array<_Tp, _Size>&& __a) _NOEXCEPT |
| 486 | { |
| 487 | static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array &&)"); |
| 488 | return _VSTD::move(__a.__elems_[_Ip]); |
| 489 | } |
| 490 | |
Eric Fiselier | 73abed7 | 2017-04-16 02:50:40 +0000 | [diff] [blame] | 491 | #endif // !_LIBCPP_CXX03_LANG |
Howard Hinnant | 22e9724 | 2010-11-17 19:52:17 +0000 | [diff] [blame] | 492 | |
Marek Kurdej | 0d71567 | 2020-01-30 13:27:35 +0100 | [diff] [blame] | 493 | #if _LIBCPP_STD_VER > 17 |
| 494 | |
| 495 | template <typename _Tp, size_t _Size, size_t... _Index> |
| 496 | _LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size> |
| 497 | __to_array_lvalue_impl(_Tp (&__arr)[_Size], index_sequence<_Index...>) { |
| 498 | return {{__arr[_Index]...}}; |
| 499 | } |
| 500 | |
| 501 | template <typename _Tp, size_t _Size, size_t... _Index> |
| 502 | _LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size> |
| 503 | __to_array_rvalue_impl(_Tp(&&__arr)[_Size], index_sequence<_Index...>) { |
| 504 | return {{_VSTD::move(__arr[_Index])...}}; |
| 505 | } |
| 506 | |
| 507 | template <typename _Tp, size_t _Size> |
| 508 | _LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size> |
| 509 | to_array(_Tp (&__arr)[_Size]) noexcept(is_nothrow_constructible_v<_Tp, _Tp&>) { |
| 510 | static_assert( |
| 511 | !is_array_v<_Tp>, |
| 512 | "[array.creation]/1: to_array does not accept multidimensional arrays."); |
| 513 | static_assert( |
| 514 | is_constructible_v<_Tp, _Tp&>, |
| 515 | "[array.creation]/1: to_array requires copy constructible elements."); |
| 516 | return __to_array_lvalue_impl(__arr, make_index_sequence<_Size>()); |
| 517 | } |
| 518 | |
| 519 | template <typename _Tp, size_t _Size> |
| 520 | _LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size> |
| 521 | to_array(_Tp(&&__arr)[_Size]) noexcept(is_nothrow_move_constructible_v<_Tp>) { |
| 522 | static_assert( |
| 523 | !is_array_v<_Tp>, |
| 524 | "[array.creation]/4: to_array does not accept multidimensional arrays."); |
| 525 | static_assert( |
| 526 | is_move_constructible_v<_Tp>, |
| 527 | "[array.creation]/4: to_array requires move constructible elements."); |
| 528 | return __to_array_rvalue_impl(_VSTD::move(__arr), |
| 529 | make_index_sequence<_Size>()); |
| 530 | } |
| 531 | |
| 532 | #endif // _LIBCPP_STD_VER > 17 |
| 533 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 534 | _LIBCPP_END_NAMESPACE_STD |
| 535 | |
| 536 | #endif // _LIBCPP_ARRAY |