Neil MacIntosh | cec26a2 | 2016-02-24 11:26:28 -0800 | [diff] [blame] | 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // Copyright (c) 2015 Microsoft Corporation. All rights reserved. |
| 4 | // |
| 5 | // This code is licensed under the MIT License (MIT). |
| 6 | // |
| 7 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 8 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 9 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 10 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 11 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 12 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 13 | // THE SOFTWARE. |
| 14 | // |
| 15 | /////////////////////////////////////////////////////////////////////////////// |
| 16 | |
| 17 | #pragma once |
| 18 | |
| 19 | #ifndef GSL_SPAN_H |
| 20 | #define GSL_SPAN_H |
| 21 | |
| 22 | #include "gsl_assert.h" |
| 23 | #include "gsl_util.h" |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 24 | #include "gsl_byte.h" |
Neil MacIntosh | cec26a2 | 2016-02-24 11:26:28 -0800 | [diff] [blame] | 25 | #include <array> |
Neil MacIntosh | cec26a2 | 2016-02-24 11:26:28 -0800 | [diff] [blame] | 26 | #include <limits> |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 27 | #include <iterator> |
Neil MacIntosh | cec26a2 | 2016-02-24 11:26:28 -0800 | [diff] [blame] | 28 | #include <stdexcept> |
| 29 | #include <type_traits> |
| 30 | #include <utility> |
| 31 | |
| 32 | #ifdef _MSC_VER |
| 33 | |
| 34 | // turn off some warnings that are noisy about our Expects statements |
| 35 | #pragma warning(push) |
| 36 | #pragma warning(disable : 4127) // conditional expression is constant |
| 37 | |
| 38 | // No MSVC does constexpr fully yet |
| 39 | #pragma push_macro("constexpr") |
| 40 | #define constexpr |
| 41 | |
| 42 | // VS 2013 workarounds |
| 43 | #if _MSC_VER <= 1800 |
| 44 | |
| 45 | #define GSL_MSVC_HAS_VARIADIC_CTOR_BUG |
| 46 | #define GSL_MSVC_NO_SUPPORT_FOR_MOVE_CTOR_DEFAULT |
| 47 | |
| 48 | // noexcept is not understood |
| 49 | #ifndef GSL_THROW_ON_CONTRACT_VIOLATION |
| 50 | #pragma push_macro("noexcept") |
| 51 | #define noexcept /* nothing */ |
| 52 | #endif |
| 53 | |
| 54 | // turn off some misguided warnings |
| 55 | #pragma warning(push) |
| 56 | #pragma warning(disable : 4351) // warns about newly introduced aggregate initializer behavior |
| 57 | #pragma warning(disable : 4512) // warns that assignment op could not be generated |
| 58 | |
| 59 | #endif // _MSC_VER <= 1800 |
| 60 | |
| 61 | #endif // _MSC_VER |
| 62 | |
| 63 | #ifdef GSL_THROW_ON_CONTRACT_VIOLATION |
| 64 | |
| 65 | #ifdef _MSC_VER |
| 66 | #pragma push_macro("noexcept") |
| 67 | #endif |
| 68 | |
| 69 | #define noexcept /* nothing */ |
| 70 | |
| 71 | #endif // GSL_THROW_ON_CONTRACT_VIOLATION |
| 72 | |
| 73 | namespace gsl |
| 74 | { |
| 75 | |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 76 | template <class ElementType, std::ptrdiff_t Extent = dynamic_extent> |
| 77 | class span; |
| 78 | |
| 79 | |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 80 | // [views.constants], constants |
| 81 | constexpr const std::ptrdiff_t dynamic_extent = -1; |
| 82 | |
| 83 | |
| 84 | // implementation details |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 85 | namespace details |
| 86 | { |
| 87 | template <class T> |
| 88 | struct is_span_oracle : std::false_type |
| 89 | { |
| 90 | }; |
| 91 | |
| 92 | template <class ElementType, std::ptrdiff_t Extent> |
| 93 | struct is_span_oracle<gsl::span<ElementType, Extent>> : std::true_type |
| 94 | { |
| 95 | }; |
| 96 | |
| 97 | template <class T> |
| 98 | struct is_span : is_span_oracle<std::remove_cv_t<T>> |
| 99 | { |
| 100 | }; |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 101 | |
| 102 | template <class From, class To> |
| 103 | struct is_allowed_pointer_conversion |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 104 | : std::bool_constant< |
| 105 | std::is_pointer<From>::value && |
| 106 | std::is_pointer<To>::value && |
| 107 | std::is_convertible<From, To>::value |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 108 | > |
| 109 | { |
| 110 | }; |
| 111 | |
| 112 | template <class From, class To> |
| 113 | struct is_allowed_integral_conversion |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 114 | : std::bool_constant< |
| 115 | std::is_integral<From>::value && |
| 116 | std::is_integral<To>::value && |
| 117 | sizeof(From) == sizeof(To) && |
| 118 | alignof(From) == alignof(To) && |
| 119 | std::is_convertible<From, To>::value |
| 120 | > |
| 121 | { |
| 122 | }; |
| 123 | |
| 124 | template <std::ptrdiff_t From, std::ptrdiff_t To> |
| 125 | struct is_allowed_extent_conversion |
| 126 | : std::bool_constant< |
| 127 | From == To || |
| 128 | From == gsl::dynamic_extent || |
| 129 | To == gsl::dynamic_extent |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 130 | > |
| 131 | { |
| 132 | }; |
| 133 | |
| 134 | template <class From, class To> |
| 135 | struct is_allowed_element_type_conversion |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 136 | : std::bool_constant< |
| 137 | std::is_same<From, std::remove_cv_t<To>>::value || |
| 138 | is_allowed_pointer_conversion<From, To>::value || |
| 139 | is_allowed_integral_conversion<From, To>::value |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 140 | > |
| 141 | { |
| 142 | }; |
| 143 | |
| 144 | template <class From> |
Neil MacIntosh | ba8ebef | 2016-05-29 17:06:29 -0700 | [diff] [blame] | 145 | struct is_allowed_element_type_conversion<From, byte> |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 146 | : std::bool_constant<!std::is_const<From>::value> |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 147 | { |
| 148 | }; |
| 149 | |
| 150 | template <class From> |
Neil MacIntosh | ba8ebef | 2016-05-29 17:06:29 -0700 | [diff] [blame] | 151 | struct is_allowed_element_type_conversion<From, const byte> |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 152 | : std::true_type |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 153 | { |
| 154 | }; |
| 155 | |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 156 | template <class Span> |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 157 | class const_span_iterator |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 158 | { |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 159 | public: |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 160 | using iterator_category = std::random_access_iterator_tag; |
| 161 | using value_type = typename Span::element_type; |
| 162 | using difference_type = std::ptrdiff_t; |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 163 | |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 164 | using const_pointer = std::add_const_t<value_type*>; |
| 165 | using pointer = const_pointer; |
| 166 | |
| 167 | using const_reference = std::add_const_t<value_type&>; |
| 168 | using reference = const_reference; |
| 169 | |
| 170 | constexpr const_span_iterator() : const_span_iterator(nullptr, 0) {} |
| 171 | constexpr const_span_iterator(const Span* span, typename Span::index_type index) : span_(span), index_(index) |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 172 | { |
| 173 | Expects(span == nullptr || (index_ >= 0 && index <= span_->length())); |
| 174 | } |
| 175 | |
Neil MacIntosh | 7b00172 | 2016-06-20 01:41:49 -0700 | [diff] [blame] | 176 | constexpr reference operator*() const { Expects(span_); return (*span_)[index_]; } |
| 177 | constexpr pointer operator->() const { Expects(span_); return &((*span_)[index_]); } |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 178 | |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 179 | constexpr const_span_iterator& operator++() noexcept |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 180 | { |
| 181 | Expects(span_ && index_ >= 0 && index_ < span_->length()); |
| 182 | ++index_; |
| 183 | return *this; |
| 184 | } |
| 185 | |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 186 | constexpr const_span_iterator operator++(int) noexcept |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 187 | { |
| 188 | auto ret = *this; |
| 189 | ++(*this); |
| 190 | return ret; |
| 191 | } |
| 192 | |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 193 | constexpr const_span_iterator& operator--() noexcept |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 194 | { |
Neil MacIntosh | 30a038c | 2016-07-18 11:38:01 -0700 | [diff] [blame] | 195 | Expects(span_ && index_ > 0 && index_ <= span_->length()); |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 196 | --index_; |
| 197 | return *this; |
| 198 | } |
| 199 | |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 200 | constexpr const_span_iterator operator--(int) noexcept |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 201 | { |
| 202 | auto ret = *this; |
| 203 | --(*this); |
| 204 | return ret; |
| 205 | } |
| 206 | |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 207 | constexpr const_span_iterator operator+(difference_type n) const noexcept |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 208 | { |
| 209 | auto ret{*this}; |
| 210 | return ret += n; |
| 211 | } |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 212 | |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 213 | constexpr const_span_iterator& operator+=(difference_type n) noexcept |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 214 | { |
Neil MacIntosh | 520c72d | 2016-07-18 12:00:33 -0700 | [diff] [blame] | 215 | Expects(span_ && (index_ + n) >= 0 && (index_ + n) <= span_->length()); |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 216 | index_ += n; |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 217 | return *this; |
| 218 | } |
| 219 | |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 220 | constexpr const_span_iterator operator-(difference_type n) const noexcept |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 221 | { |
| 222 | auto ret{*this}; |
| 223 | return ret -= n; |
| 224 | } |
| 225 | |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 226 | constexpr const_span_iterator& operator-=(difference_type n) noexcept |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 227 | { |
| 228 | return *this += -n; |
| 229 | } |
| 230 | |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 231 | constexpr difference_type operator-(const const_span_iterator& rhs) const noexcept |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 232 | { |
| 233 | Expects(span_ == rhs.span_); |
| 234 | return index_ - rhs.index_; |
| 235 | } |
| 236 | |
Neil MacIntosh | 7b00172 | 2016-06-20 01:41:49 -0700 | [diff] [blame] | 237 | constexpr reference operator[](difference_type n) const noexcept |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 238 | { |
| 239 | return *(*this + n); |
| 240 | } |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 241 | |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 242 | constexpr bool operator==(const const_span_iterator& rhs) const noexcept |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 243 | { |
| 244 | return span_ == rhs.span_ && index_ == rhs.index_; |
| 245 | } |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 246 | |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 247 | constexpr bool operator!=(const const_span_iterator& rhs) const noexcept { return !(*this == rhs); } |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 248 | |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 249 | constexpr bool operator<(const const_span_iterator& rhs) const noexcept |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 250 | { |
| 251 | Expects(span_ == rhs.span_); |
| 252 | return index_ < rhs.index_; |
| 253 | } |
| 254 | |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 255 | constexpr bool operator<=(const const_span_iterator& rhs) const noexcept { return !(rhs < *this); } |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 256 | |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 257 | constexpr bool operator>(const const_span_iterator& rhs) const noexcept { return rhs < *this; } |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 258 | |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 259 | constexpr bool operator>=(const const_span_iterator& rhs) const noexcept { return !(rhs > *this); } |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 260 | |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 261 | void swap(const_span_iterator& rhs) noexcept |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 262 | { |
| 263 | std::swap(index_, rhs.index_); |
| 264 | std::swap(m_span, rhs.m_span); |
| 265 | } |
| 266 | |
| 267 | private: |
| 268 | const Span* span_; |
| 269 | ptrdiff_t index_; |
| 270 | }; |
| 271 | |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 272 | |
| 273 | template <class Span> |
| 274 | class span_iterator : public const_span_iterator<Span> |
| 275 | { |
| 276 | using base_type = const_span_iterator<Span>; |
| 277 | |
| 278 | public: |
| 279 | using iterator_category = std::random_access_iterator_tag; |
| 280 | using value_type = typename Span::element_type; |
| 281 | using difference_type = std::ptrdiff_t; |
| 282 | |
| 283 | using pointer = value_type*; |
| 284 | using reference = value_type&; |
| 285 | |
| 286 | constexpr span_iterator() : base_type() {} |
| 287 | constexpr span_iterator(const Span* span, typename Span::index_type index) : base_type(span, index) {} |
| 288 | |
Neil MacIntosh | 0c1b671 | 2016-07-20 08:52:09 -0700 | [diff] [blame^] | 289 | constexpr reference operator*() const { return const_cast<reference>(base_type::operator*()); } |
| 290 | constexpr pointer operator->() const { return const_cast<pointer>(base_type::operator->()); } |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 291 | |
| 292 | constexpr span_iterator& operator++() noexcept { base_type::operator++(); return *this; } |
| 293 | |
| 294 | constexpr span_iterator operator++(int) noexcept { return base_type::operator++(1); } |
| 295 | |
| 296 | constexpr span_iterator& operator--() noexcept { base_type::operator--(); return *this; } |
| 297 | |
| 298 | constexpr span_iterator operator--(int) noexcept { return base_type::operator--(1); } |
| 299 | |
| 300 | constexpr span_iterator operator+(difference_type n) const noexcept { return base_type::operator+(n); } |
| 301 | |
| 302 | constexpr span_iterator& operator+=(difference_type n) noexcept { return base_type::operator+=(n); } |
| 303 | |
| 304 | constexpr span_iterator operator-(difference_type n) const noexcept { return base_type::operator-(n); } |
| 305 | |
| 306 | constexpr span_iterator& operator-=(difference_type n) noexcept { return base_type::operator-=(n); } |
| 307 | |
| 308 | constexpr difference_type operator-(const span_iterator& rhs) const noexcept { return base_type::operator-(rhs); } |
| 309 | |
| 310 | constexpr reference operator[](difference_type n) const noexcept { return *(*this + n); } |
| 311 | |
| 312 | constexpr bool operator==(const span_iterator& rhs) const noexcept { return base_type::operator==(rhs); } |
| 313 | |
| 314 | constexpr bool operator!=(const span_iterator& rhs) const noexcept { return !(*this == rhs); } |
| 315 | |
| 316 | constexpr bool operator<(const span_iterator& rhs) const noexcept { return base_type::operator<(rhs); } |
| 317 | |
| 318 | constexpr bool operator<=(const span_iterator& rhs) const noexcept { return !(rhs < *this); } |
| 319 | |
| 320 | constexpr bool operator>(const span_iterator& rhs) const noexcept { return rhs < *this; } |
| 321 | |
| 322 | constexpr bool operator>=(const span_iterator& rhs) const noexcept { return !(rhs > *this); } |
| 323 | |
| 324 | void swap(span_iterator& rhs) noexcept { base_type::swap(rhs); } |
| 325 | }; |
| 326 | |
| 327 | template <typename Span> |
| 328 | constexpr const_span_iterator<Span> operator+(typename const_span_iterator<Span>::difference_type n, |
| 329 | const const_span_iterator<Span>& rhs) noexcept |
| 330 | { |
| 331 | return rhs + n; |
| 332 | } |
| 333 | |
| 334 | template <typename Span> |
| 335 | constexpr const_span_iterator<Span> operator-(typename const_span_iterator<Span>::difference_type n, |
| 336 | const const_span_iterator<Span>& rhs) noexcept |
| 337 | { |
| 338 | return rhs - n; |
| 339 | } |
| 340 | |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 341 | template <typename Span> |
Neil MacIntosh | 7b00172 | 2016-06-20 01:41:49 -0700 | [diff] [blame] | 342 | constexpr span_iterator<Span> operator+(typename span_iterator<Span>::difference_type n, |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 343 | const span_iterator<Span>& rhs) noexcept |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 344 | { |
| 345 | return rhs + n; |
| 346 | } |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 347 | |
| 348 | template <typename Span> |
Neil MacIntosh | 7b00172 | 2016-06-20 01:41:49 -0700 | [diff] [blame] | 349 | constexpr span_iterator<Span> operator-(typename span_iterator<Span>::difference_type n, |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 350 | const span_iterator<Span>& rhs) noexcept |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 351 | { |
| 352 | return rhs - n; |
| 353 | } |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 354 | |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 355 | } // namespace details |
| 356 | |
| 357 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 358 | // [span], class template span |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 359 | template <class ElementType, std::ptrdiff_t Extent> |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 360 | class span { |
| 361 | public: |
| 362 | // constants and types |
| 363 | using element_type = ElementType; |
| 364 | using index_type = std::ptrdiff_t; |
| 365 | using pointer = element_type*; |
| 366 | using reference = element_type&; |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 367 | |
| 368 | using iterator = details::span_iterator<span<ElementType, Extent>>; |
Neil MacIntosh | 30a038c | 2016-07-18 11:38:01 -0700 | [diff] [blame] | 369 | using const_iterator = details::const_span_iterator<span>; |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 370 | using reverse_iterator = std::reverse_iterator<iterator>; |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 371 | using const_reverse_iterator = std::reverse_iterator<const_iterator>; |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 372 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 373 | constexpr static const index_type extent = Extent; |
| 374 | |
| 375 | // [span.cons], span constructors, copy, assignment, and destructor |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 376 | constexpr span() noexcept : storage_(nullptr, extent_type<0>()) |
| 377 | {} |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 378 | |
| 379 | constexpr span(nullptr_t) noexcept : span() |
| 380 | {} |
| 381 | |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 382 | constexpr span(pointer ptr, index_type count) : storage_(ptr, count) |
Neil MacIntosh | 25ff7ec | 2016-05-29 13:54:19 -0700 | [diff] [blame] | 383 | { Expects((!ptr && count == 0) || (ptr && count >= 0)); } |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 384 | |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 385 | constexpr span(pointer firstElem, pointer lastElem) |
| 386 | : storage_(firstElem, std::distance(firstElem, lastElem)) |
| 387 | {} |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 388 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 389 | template <size_t N> |
Neil MacIntosh | 7b00172 | 2016-06-20 01:41:49 -0700 | [diff] [blame] | 390 | constexpr span(element_type(&arr)[N]) noexcept |
Neil MacIntosh | f61a9bb | 2016-02-29 13:16:48 -0800 | [diff] [blame] | 391 | : storage_(&arr[0], extent_type<N>()) |
| 392 | {} |
| 393 | |
| 394 | template <size_t N> |
Neil MacIntosh | 62f3020 | 2016-06-14 20:14:17 -0700 | [diff] [blame] | 395 | constexpr span(std::array<element_type, N>& arr) |
| 396 | : storage_(&arr[0], extent_type<N>()) |
| 397 | {} |
| 398 | |
| 399 | template <size_t N, class = std::enable_if_t<is_const<element_type>::value>> |
Neil MacIntosh | f61a9bb | 2016-02-29 13:16:48 -0800 | [diff] [blame] | 400 | constexpr span(std::array<std::remove_const_t<element_type>, N>& arr) |
| 401 | : storage_(&arr[0], extent_type<N>()) |
| 402 | {} |
| 403 | |
Neil MacIntosh | 3d4c349 | 2016-03-17 17:20:33 -0700 | [diff] [blame] | 404 | template <size_t N, class = std::enable_if_t<is_const<element_type>::value>> |
Neil MacIntosh | f61a9bb | 2016-02-29 13:16:48 -0800 | [diff] [blame] | 405 | constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr) |
| 406 | : storage_(&arr[0], extent_type<N>()) |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 407 | {} |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 408 | |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 409 | // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement |
| 410 | // on Container to be a contiguous sequence container. |
| 411 | template <class Container, |
| 412 | class = std::enable_if_t<!details::is_span<Container>::value && |
| 413 | std::is_convertible<Container::pointer, pointer>::value && |
| 414 | std::is_convertible<Container::pointer, decltype(std::declval<Container>().data())>::value> |
| 415 | > |
| 416 | constexpr span(Container& cont) : span(cont.data(), cont.size()) {} |
| 417 | |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 418 | template <class Container, |
Neil MacIntosh | 3d4c349 | 2016-03-17 17:20:33 -0700 | [diff] [blame] | 419 | class = std::enable_if_t<std::is_const<element_type>::value && |
| 420 | !details::is_span<Container>::value && |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 421 | std::is_convertible<Container::pointer, pointer>::value && |
| 422 | std::is_convertible<Container::pointer, decltype(std::declval<Container>().data())>::value> |
| 423 | > |
Neil MacIntosh | 3d4c349 | 2016-03-17 17:20:33 -0700 | [diff] [blame] | 424 | constexpr span(const Container& cont) : span(cont.data(), cont.size()) {} |
| 425 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 426 | constexpr span(const span& other) noexcept = default; |
| 427 | constexpr span(span&& other) noexcept = default; |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 428 | |
| 429 | template <class OtherElementType, std::ptrdiff_t OtherExtent, |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 430 | class = std::enable_if_t< |
| 431 | details::is_allowed_extent_conversion<OtherExtent, Extent>::value && |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 432 | details::is_allowed_element_type_conversion<OtherElementType, element_type>::value |
| 433 | > |
| 434 | > |
| 435 | constexpr span(const span<OtherElementType, OtherExtent>& other) |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 436 | : storage_(reinterpret_cast<pointer>(other.data()), extent_type<OtherExtent>(other.size())) |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 437 | {} |
| 438 | |
| 439 | template <class OtherElementType, std::ptrdiff_t OtherExtent, |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 440 | class = std::enable_if_t< |
| 441 | details::is_allowed_extent_conversion<OtherExtent, Extent>::value && |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 442 | details::is_allowed_element_type_conversion<OtherElementType, element_type>::value |
| 443 | > |
| 444 | > |
| 445 | constexpr span(span<OtherElementType, OtherExtent>&& other) |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 446 | : storage_(reinterpret_cast<pointer>(other.data()), extent_type<OtherExtent>(other.size())) |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 447 | {} |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 448 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 449 | ~span() noexcept = default; |
| 450 | constexpr span& operator=(const span& other) noexcept = default; |
| 451 | constexpr span& operator=(span&& other) noexcept = default; |
| 452 | |
| 453 | // [span.sub], span subviews |
| 454 | template <ptrdiff_t Count> |
Neil MacIntosh | c8a412f | 2016-03-18 16:49:29 -0700 | [diff] [blame] | 455 | constexpr span<element_type, Count> first() const |
| 456 | { |
| 457 | Expects(Count >= 0 && Count <= size()); |
| 458 | return { data(), Count }; |
| 459 | } |
| 460 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 461 | template <ptrdiff_t Count> |
Neil MacIntosh | c8a412f | 2016-03-18 16:49:29 -0700 | [diff] [blame] | 462 | constexpr span<element_type, Count> last() const |
| 463 | { |
| 464 | Expects(Count >= 0 && Count <= size()); |
Neil MacIntosh | 64598bc | 2016-06-14 20:20:09 -0700 | [diff] [blame] | 465 | return{ data() + (size() - Count), Count }; |
Neil MacIntosh | c8a412f | 2016-03-18 16:49:29 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 468 | template <ptrdiff_t Offset, ptrdiff_t Count = dynamic_extent> |
Neil MacIntosh | c8a412f | 2016-03-18 16:49:29 -0700 | [diff] [blame] | 469 | constexpr span<element_type, Count> subspan() const |
| 470 | { |
| 471 | Expects((Offset == 0 || Offset > 0 && Offset <= size()) && |
| 472 | (Count == dynamic_extent || Count >= 0 && Offset + Count <= size())); |
| 473 | return { data() + Offset, Count == dynamic_extent ? size() - Offset : Count }; |
| 474 | } |
| 475 | |
| 476 | constexpr span<element_type, dynamic_extent> first(index_type count) const |
| 477 | { |
| 478 | Expects(count >= 0 && count <= size()); |
| 479 | return { data(), count }; |
| 480 | } |
| 481 | |
| 482 | constexpr span<element_type, dynamic_extent> last(index_type count) const |
| 483 | { |
| 484 | Expects(count >= 0 && count <= size()); |
Neil MacIntosh | 64598bc | 2016-06-14 20:20:09 -0700 | [diff] [blame] | 485 | return { data() + (size() - count), count }; |
Neil MacIntosh | c8a412f | 2016-03-18 16:49:29 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | constexpr span<element_type, dynamic_extent> subspan(index_type offset, |
| 489 | index_type count = dynamic_extent) const |
| 490 | { |
| 491 | Expects((offset == 0 || offset > 0 && offset <= size()) && |
| 492 | (count == dynamic_extent || count >= 0 && offset + count <= size())); |
| 493 | return { data() + offset, count == dynamic_extent ? size() - offset : count }; |
| 494 | } |
| 495 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 496 | // [span.obs], span observers |
| 497 | constexpr index_type length() const noexcept { return size(); } |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 498 | constexpr index_type size() const noexcept { return storage_.size(); } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 499 | constexpr index_type length_bytes() const noexcept { return size_bytes(); } |
| 500 | constexpr index_type size_bytes() const noexcept { return size() * sizeof(element_type); } |
| 501 | constexpr bool empty() const noexcept { return size() == 0; } |
| 502 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 503 | // [span.elem], span element access |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 504 | constexpr reference operator[](index_type idx) const |
| 505 | { |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 506 | Expects(idx >= 0 && idx < storage_.size()); |
| 507 | return storage_.data()[idx]; |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 508 | } |
| 509 | constexpr reference operator()(index_type idx) const { return this->operator[](idx); } |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 510 | constexpr pointer data() const noexcept { return storage_.data(); } |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 511 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 512 | // [span.iter], span iterator support |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 513 | iterator begin() const noexcept { return {this, 0}; } |
| 514 | iterator end() const noexcept { return {this, length()}; } |
Neil MacIntosh | 30a038c | 2016-07-18 11:38:01 -0700 | [diff] [blame] | 515 | |
| 516 | const_iterator cbegin() const noexcept { return {this, 0}; } |
| 517 | const_iterator cend() const noexcept { return {this, length()}; } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 518 | |
Neil MacIntosh | 30a038c | 2016-07-18 11:38:01 -0700 | [diff] [blame] | 519 | reverse_iterator rbegin() const noexcept { return reverse_iterator{{this, length()}}; } |
| 520 | reverse_iterator rend() const noexcept { return reverse_iterator{{this, 0}}; } |
| 521 | |
| 522 | const_reverse_iterator crbegin() const noexcept { return reverse_iterator{{this, length()}}; } |
| 523 | const_reverse_iterator crend() const noexcept { return reverse_iterator{{this, 0}}; } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 524 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 525 | private: |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 526 | template <index_type Extent> |
| 527 | class extent_type; |
| 528 | |
| 529 | template <index_type Extent> |
| 530 | class extent_type |
| 531 | { |
| 532 | public: |
| 533 | static_assert(Extent >= 0, "A fixed-size span must be >= 0 in size."); |
| 534 | |
| 535 | constexpr extent_type() noexcept {} |
| 536 | |
| 537 | template <index_type Other> |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 538 | constexpr extent_type(extent_type<Other> ext) noexcept |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 539 | { |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 540 | static_assert(Other == Extent || Other == dynamic_extent, |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 541 | "Mismatch between fixed-size extent and size of initializing data."); |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 542 | Expects(ext.size() == Extent); |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | constexpr extent_type(index_type size) |
| 546 | { Expects(size == Extent); } |
| 547 | |
| 548 | constexpr inline index_type size() const noexcept { return Extent; } |
| 549 | }; |
| 550 | |
| 551 | template <> |
| 552 | class extent_type<dynamic_extent> |
| 553 | { |
| 554 | public: |
| 555 | template <index_type Other> |
| 556 | explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size()) |
| 557 | {} |
| 558 | |
| 559 | explicit constexpr extent_type(index_type size) : size_(size) |
| 560 | { Expects(size >= 0); } |
| 561 | |
| 562 | constexpr inline index_type size() const noexcept |
| 563 | { return size_; } |
| 564 | |
| 565 | private: |
| 566 | index_type size_; |
| 567 | }; |
| 568 | |
| 569 | // this implementation detail class lets us take advantage of the |
| 570 | // empty base class optimization to pay for only storage of a single |
| 571 | // pointer in the case of fixed-size spans |
| 572 | template <class ExtentType> |
| 573 | class storage_type : public ExtentType |
| 574 | { |
| 575 | public: |
| 576 | template <class OtherExtentType> |
Neil MacIntosh | 7b00172 | 2016-06-20 01:41:49 -0700 | [diff] [blame] | 577 | constexpr storage_type(pointer data, OtherExtentType ext) |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 578 | : ExtentType(ext), data_(data) {} |
| 579 | |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 580 | constexpr inline pointer data() const noexcept |
| 581 | { return data_; } |
| 582 | |
| 583 | private: |
| 584 | pointer data_; |
| 585 | }; |
| 586 | |
| 587 | storage_type<extent_type<Extent>> storage_; |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 588 | }; |
| 589 | |
| 590 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 591 | // [span.comparison], span comparison operators |
| 592 | template <class ElementType, ptrdiff_t Extent> |
Neil MacIntosh | 7b00172 | 2016-06-20 01:41:49 -0700 | [diff] [blame] | 593 | constexpr bool operator==(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) |
Neil MacIntosh | d63c980 | 2016-05-29 14:05:09 -0700 | [diff] [blame] | 594 | { return std::equal(l.begin(), l.end(), r.begin(), r.end()); } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 595 | |
| 596 | template <class ElementType, ptrdiff_t Extent> |
Neil MacIntosh | 7b00172 | 2016-06-20 01:41:49 -0700 | [diff] [blame] | 597 | constexpr bool operator!=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) |
Neil MacIntosh | d63c980 | 2016-05-29 14:05:09 -0700 | [diff] [blame] | 598 | { return !(l == r); } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 599 | |
| 600 | template <class ElementType, ptrdiff_t Extent> |
Neil MacIntosh | 7b00172 | 2016-06-20 01:41:49 -0700 | [diff] [blame] | 601 | constexpr bool operator<(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) |
Neil MacIntosh | d63c980 | 2016-05-29 14:05:09 -0700 | [diff] [blame] | 602 | { return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 603 | |
| 604 | template <class ElementType, ptrdiff_t Extent> |
Neil MacIntosh | 7b00172 | 2016-06-20 01:41:49 -0700 | [diff] [blame] | 605 | constexpr bool operator<=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) |
Neil MacIntosh | d63c980 | 2016-05-29 14:05:09 -0700 | [diff] [blame] | 606 | { return !(l > r); } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 607 | |
| 608 | template <class ElementType, ptrdiff_t Extent> |
Neil MacIntosh | 7b00172 | 2016-06-20 01:41:49 -0700 | [diff] [blame] | 609 | constexpr bool operator>(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) |
Neil MacIntosh | d63c980 | 2016-05-29 14:05:09 -0700 | [diff] [blame] | 610 | { return r < l; } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 611 | |
| 612 | template <class ElementType, ptrdiff_t Extent> |
Neil MacIntosh | 7b00172 | 2016-06-20 01:41:49 -0700 | [diff] [blame] | 613 | constexpr bool operator>=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) |
Neil MacIntosh | d63c980 | 2016-05-29 14:05:09 -0700 | [diff] [blame] | 614 | { return !(l < r); } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 615 | |
| 616 | |
Neil MacIntosh | ba8ebef | 2016-05-29 17:06:29 -0700 | [diff] [blame] | 617 | namespace details |
| 618 | { |
| 619 | // if we only supported compilers with good constexpr support then |
| 620 | // this pair of classes could collapse down to a constexpr function |
| 621 | |
| 622 | // we should use a narrow_cast<> to go to size_t, but older compilers may not see it as constexpr |
| 623 | // and so will fail compilation of the template |
| 624 | template <class ElementType, ptrdiff_t Extent> |
| 625 | struct calculate_byte_size : |
| 626 | std::integral_constant<std::ptrdiff_t, static_cast<ptrdiff_t>(sizeof(ElementType) * static_cast<size_t>(Extent))> |
| 627 | {}; |
| 628 | |
| 629 | template <class ElementType> |
| 630 | struct calculate_byte_size<ElementType, dynamic_extent> : |
| 631 | std::integral_constant<std::ptrdiff_t, dynamic_extent> |
| 632 | {}; |
| 633 | } |
| 634 | |
| 635 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 636 | // [span.objectrep], views of object representation |
| 637 | template <class ElementType, ptrdiff_t Extent> |
Neil MacIntosh | 7b00172 | 2016-06-20 01:41:49 -0700 | [diff] [blame] | 638 | span<const byte, details::calculate_byte_size<ElementType, Extent>::value> as_bytes(span<ElementType, Extent> s) noexcept |
Neil MacIntosh | ba8ebef | 2016-05-29 17:06:29 -0700 | [diff] [blame] | 639 | { return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()}; } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 640 | |
Neil MacIntosh | ba8ebef | 2016-05-29 17:06:29 -0700 | [diff] [blame] | 641 | template <class ElementType, ptrdiff_t Extent, class = std::enable_if_t<!std::is_const<ElementType>::value>> |
Neil MacIntosh | 7b00172 | 2016-06-20 01:41:49 -0700 | [diff] [blame] | 642 | span<byte, details::calculate_byte_size<ElementType, Extent>::value> as_writeable_bytes(span<ElementType, Extent> s) noexcept |
Neil MacIntosh | ba8ebef | 2016-05-29 17:06:29 -0700 | [diff] [blame] | 643 | { return {reinterpret_cast<byte*>(s.data()), s.size_bytes()}; } |
Neil MacIntosh | cec26a2 | 2016-02-24 11:26:28 -0800 | [diff] [blame] | 644 | |
| 645 | } // namespace gsl |
| 646 | |
| 647 | #ifdef _MSC_VER |
| 648 | |
| 649 | #undef constexpr |
| 650 | #pragma pop_macro("constexpr") |
| 651 | |
| 652 | #if _MSC_VER <= 1800 |
| 653 | #pragma warning(pop) |
| 654 | |
| 655 | #ifndef GSL_THROW_ON_CONTRACT_VIOLATION |
| 656 | #undef noexcept |
| 657 | #pragma pop_macro("noexcept") |
| 658 | #endif // GSL_THROW_ON_CONTRACT_VIOLATION |
| 659 | |
| 660 | #undef GSL_MSVC_HAS_VARIADIC_CTOR_BUG |
| 661 | |
| 662 | #endif // _MSC_VER <= 1800 |
| 663 | |
| 664 | #endif // _MSC_VER |
| 665 | |
| 666 | #if defined(GSL_THROW_ON_CONTRACT_VIOLATION) |
| 667 | |
| 668 | #undef noexcept |
| 669 | |
| 670 | #ifdef _MSC_VER |
| 671 | #pragma warning(pop) |
| 672 | #pragma pop_macro("noexcept") |
| 673 | #endif |
| 674 | |
| 675 | #endif // GSL_THROW_ON_CONTRACT_VIOLATION |
| 676 | |
| 677 | #endif // GSL_SPAN_H |