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