span 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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. #ifndef GSL_SPAN_H
  17. #define GSL_SPAN_H
  18. #include "assert" // for Expects
  19. #include "byte" // for byte
  20. #include "span_ext" // for span specialization of gsl::at and other span-related extensions
  21. #include "util" // for narrow_cast
  22. #include <array> // for array
  23. #include <cstddef> // for ptrdiff_t, size_t, nullptr_t
  24. #include <iterator> // for reverse_iterator, distance, random_access_...
  25. #include <memory> // for pointer_traits
  26. #include <type_traits> // for enable_if_t, declval, is_convertible, inte...
  27. #if defined(__has_include) && __has_include(<version>)
  28. #include <version>
  29. #endif
  30. #if defined(_MSC_VER) && !defined(__clang__)
  31. #pragma warning(push)
  32. // turn off some warnings that are noisy about our Expects statements
  33. #pragma warning(disable : 4127) // conditional expression is constant
  34. #pragma warning( \
  35. disable : 4146) // unary minus operator applied to unsigned type, result still unsigned
  36. #pragma warning(disable : 4702) // unreachable code
  37. // Turn MSVC /analyze rules that generate too much noise. TODO: fix in the tool.
  38. #pragma warning(disable : 26495) // uninitialized member when constructor calls constructor
  39. #pragma warning(disable : 26446) // parser bug does not allow attributes on some templates
  40. #endif // _MSC_VER
  41. // See if we have enough C++17 power to use a static constexpr data member
  42. // without needing an out-of-line definition
  43. #if !(defined(__cplusplus) && (__cplusplus >= 201703L))
  44. #define GSL_USE_STATIC_CONSTEXPR_WORKAROUND
  45. #endif // !(defined(__cplusplus) && (__cplusplus >= 201703L))
  46. // GCC 7 does not like the signed unsigned mismatch (size_t ptrdiff_t)
  47. // While there is a conversion from signed to unsigned, it happens at
  48. // compiletime, so the compiler wouldn't have to warn indiscriminately, but
  49. // could check if the source value actually doesn't fit into the target type
  50. // and only warn in those cases.
  51. #if defined(__GNUC__) && __GNUC__ > 6
  52. #pragma GCC diagnostic push
  53. #pragma GCC diagnostic ignored "-Wsign-conversion"
  54. #endif
  55. // Turn off clang unsafe buffer warnings as all accessed are guarded by runtime checks
  56. #if defined(__clang__)
  57. #if __has_warning("-Wunsafe-buffer-usage")
  58. #pragma clang diagnostic push
  59. #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
  60. #endif // __has_warning("-Wunsafe-buffer-usage")
  61. #endif // defined(__clang__)
  62. namespace gsl
  63. {
  64. // implementation details
  65. namespace details
  66. {
  67. template <class T>
  68. struct is_span_oracle : std::false_type
  69. {
  70. };
  71. template <class ElementType, std::size_t Extent>
  72. struct is_span_oracle<gsl::span<ElementType, Extent>> : std::true_type
  73. {
  74. };
  75. template <class T>
  76. struct is_span : public is_span_oracle<std::remove_cv_t<T>>
  77. {
  78. };
  79. template <class T>
  80. struct is_std_array_oracle : std::false_type
  81. {
  82. };
  83. template <class ElementType, std::size_t Extent>
  84. struct is_std_array_oracle<std::array<ElementType, Extent>> : std::true_type
  85. {
  86. };
  87. template <class T>
  88. struct is_std_array : is_std_array_oracle<std::remove_cv_t<T>>
  89. {
  90. };
  91. template <std::size_t From, std::size_t To>
  92. struct is_allowed_extent_conversion
  93. : std::integral_constant<bool, From == To || To == dynamic_extent>
  94. {
  95. };
  96. template <class From, class To>
  97. struct is_allowed_element_type_conversion
  98. : std::integral_constant<bool, std::is_convertible<From (*)[], To (*)[]>::value>
  99. {
  100. };
  101. template <class Type>
  102. class span_iterator
  103. {
  104. public:
  105. #if defined(__cpp_lib_ranges) || (defined(_MSVC_STL_VERSION) && defined(__cpp_lib_concepts))
  106. using iterator_concept = std::contiguous_iterator_tag;
  107. #endif // __cpp_lib_ranges
  108. using iterator_category = std::random_access_iterator_tag;
  109. using value_type = std::remove_cv_t<Type>;
  110. using difference_type = std::ptrdiff_t;
  111. using pointer = Type*;
  112. using reference = Type&;
  113. #ifdef _MSC_VER
  114. using _Unchecked_type = pointer;
  115. using _Prevent_inheriting_unwrap = span_iterator;
  116. #endif // _MSC_VER
  117. constexpr span_iterator() = default;
  118. constexpr span_iterator(pointer begin, pointer end, pointer current)
  119. : begin_(begin), end_(end), current_(current)
  120. {}
  121. constexpr operator span_iterator<const Type>() const noexcept
  122. {
  123. return {begin_, end_, current_};
  124. }
  125. constexpr reference operator*() const noexcept
  126. {
  127. Expects(begin_ && end_);
  128. Expects(begin_ <= current_ && current_ < end_);
  129. return *current_;
  130. }
  131. constexpr pointer operator->() const noexcept
  132. {
  133. Expects(begin_ && end_);
  134. Expects(begin_ <= current_ && current_ < end_);
  135. return current_;
  136. }
  137. constexpr span_iterator& operator++() noexcept
  138. {
  139. Expects(begin_ && current_ && end_);
  140. Expects(current_ < end_);
  141. // clang-format off
  142. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  143. // clang-format on
  144. ++current_;
  145. return *this;
  146. }
  147. constexpr span_iterator operator++(int) noexcept
  148. {
  149. span_iterator ret = *this;
  150. ++*this;
  151. return ret;
  152. }
  153. constexpr span_iterator& operator--() noexcept
  154. {
  155. Expects(begin_ && end_);
  156. Expects(begin_ < current_);
  157. --current_;
  158. return *this;
  159. }
  160. constexpr span_iterator operator--(int) noexcept
  161. {
  162. span_iterator ret = *this;
  163. --*this;
  164. return ret;
  165. }
  166. constexpr span_iterator& operator+=(const difference_type n) noexcept
  167. {
  168. if (n != 0) Expects(begin_ && current_ && end_);
  169. if (n > 0) Expects(end_ - current_ >= n);
  170. if (n < 0) Expects(current_ - begin_ >= -n);
  171. // clang-format off
  172. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  173. // clang-format on
  174. current_ += n;
  175. return *this;
  176. }
  177. constexpr span_iterator operator+(const difference_type n) const noexcept
  178. {
  179. span_iterator ret = *this;
  180. ret += n;
  181. return ret;
  182. }
  183. friend constexpr span_iterator operator+(const difference_type n,
  184. const span_iterator& rhs) noexcept
  185. {
  186. return rhs + n;
  187. }
  188. constexpr span_iterator& operator-=(const difference_type n) noexcept
  189. {
  190. if (n != 0) Expects(begin_ && current_ && end_);
  191. if (n > 0) Expects(current_ - begin_ >= n);
  192. if (n < 0) Expects(end_ - current_ >= -n);
  193. GSL_SUPPRESS(bounds .1)
  194. current_ -= n;
  195. return *this;
  196. }
  197. constexpr span_iterator operator-(const difference_type n) const noexcept
  198. {
  199. span_iterator ret = *this;
  200. ret -= n;
  201. return ret;
  202. }
  203. template <
  204. class Type2,
  205. std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
  206. constexpr difference_type operator-(const span_iterator<Type2>& rhs) const noexcept
  207. {
  208. Expects(begin_ == rhs.begin_ && end_ == rhs.end_);
  209. return current_ - rhs.current_;
  210. }
  211. constexpr reference operator[](const difference_type n) const noexcept
  212. {
  213. return *(*this + n);
  214. }
  215. template <
  216. class Type2,
  217. std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
  218. constexpr bool operator==(const span_iterator<Type2>& rhs) const noexcept
  219. {
  220. Expects(begin_ == rhs.begin_ && end_ == rhs.end_);
  221. return current_ == rhs.current_;
  222. }
  223. template <
  224. class Type2,
  225. std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
  226. constexpr bool operator!=(const span_iterator<Type2>& rhs) const noexcept
  227. {
  228. return !(*this == rhs);
  229. }
  230. template <
  231. class Type2,
  232. std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
  233. constexpr bool operator<(const span_iterator<Type2>& rhs) const noexcept
  234. {
  235. Expects(begin_ == rhs.begin_ && end_ == rhs.end_);
  236. return current_ < rhs.current_;
  237. }
  238. template <
  239. class Type2,
  240. std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
  241. constexpr bool operator>(const span_iterator<Type2>& rhs) const noexcept
  242. {
  243. return rhs < *this;
  244. }
  245. template <
  246. class Type2,
  247. std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
  248. constexpr bool operator<=(const span_iterator<Type2>& rhs) const noexcept
  249. {
  250. return !(rhs < *this);
  251. }
  252. template <
  253. class Type2,
  254. std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
  255. constexpr bool operator>=(const span_iterator<Type2>& rhs) const noexcept
  256. {
  257. return !(*this < rhs);
  258. }
  259. #ifdef _MSC_VER
  260. // MSVC++ iterator debugging support; allows STL algorithms in 15.8+
  261. // to unwrap span_iterator to a pointer type after a range check in STL
  262. // algorithm calls
  263. friend constexpr void _Verify_range(span_iterator lhs, span_iterator rhs) noexcept
  264. { // test that [lhs, rhs) forms a valid range inside an STL algorithm
  265. Expects(lhs.begin_ == rhs.begin_ // range spans have to match
  266. && lhs.end_ == rhs.end_ &&
  267. lhs.current_ <= rhs.current_); // range must not be transposed
  268. }
  269. constexpr void _Verify_offset(const difference_type n) const noexcept
  270. { // test that *this + n is within the range of this call
  271. if (n != 0) Expects(begin_ && current_ && end_);
  272. if (n > 0) Expects(end_ - current_ >= n);
  273. if (n < 0) Expects(current_ - begin_ >= -n);
  274. }
  275. // clang-format off
  276. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  277. // clang-format on
  278. constexpr pointer _Unwrapped() const noexcept
  279. { // after seeking *this to a high water mark, or using one of the
  280. // _Verify_xxx functions above, unwrap this span_iterator to a raw
  281. // pointer
  282. return current_;
  283. }
  284. // Tell the STL that span_iterator should not be unwrapped if it can't
  285. // validate in advance, even in release / optimized builds:
  286. #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
  287. static constexpr const bool _Unwrap_when_unverified = false;
  288. #else
  289. static constexpr bool _Unwrap_when_unverified = false;
  290. #endif
  291. // clang-format off
  292. GSL_SUPPRESS(con.3) // NO-FORMAT: attribute // TODO: false positive
  293. // clang-format on
  294. constexpr void _Seek_to(const pointer p) noexcept
  295. { // adjust the position of *this to previously verified location p
  296. // after _Unwrapped
  297. current_ = p;
  298. }
  299. #endif
  300. pointer begin_ = nullptr;
  301. pointer end_ = nullptr;
  302. pointer current_ = nullptr;
  303. template <typename Ptr>
  304. friend struct std::pointer_traits;
  305. };
  306. }} // namespace gsl::details
  307. namespace std
  308. {
  309. template <class Type>
  310. struct pointer_traits<::gsl::details::span_iterator<Type>>
  311. {
  312. using pointer = ::gsl::details::span_iterator<Type>;
  313. using element_type = Type;
  314. using difference_type = ptrdiff_t;
  315. static constexpr element_type* to_address(const pointer i) noexcept { return i.current_; }
  316. };
  317. } // namespace std
  318. namespace gsl { namespace details {
  319. template <std::size_t Ext>
  320. class extent_type
  321. {
  322. public:
  323. using size_type = std::size_t;
  324. constexpr extent_type() noexcept = default;
  325. constexpr explicit extent_type(extent_type<dynamic_extent>);
  326. constexpr explicit extent_type(size_type size) { Expects(size == Ext); }
  327. constexpr size_type size() const noexcept { return Ext; }
  328. private:
  329. #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
  330. static constexpr const size_type size_ = Ext; // static size equal to Ext
  331. #else
  332. static constexpr size_type size_ = Ext; // static size equal to Ext
  333. #endif
  334. };
  335. template <>
  336. class extent_type<dynamic_extent>
  337. {
  338. public:
  339. using size_type = std::size_t;
  340. template <size_type Other>
  341. constexpr explicit extent_type(extent_type<Other> ext) : size_(ext.size())
  342. {}
  343. constexpr explicit extent_type(size_type size) : size_(size)
  344. {
  345. Expects(size != dynamic_extent);
  346. }
  347. constexpr size_type size() const noexcept { return size_; }
  348. private:
  349. size_type size_;
  350. };
  351. template <std::size_t Ext>
  352. constexpr extent_type<Ext>::extent_type(extent_type<dynamic_extent> ext)
  353. {
  354. Expects(ext.size() == Ext);
  355. }
  356. template <class ElementType, std::size_t Extent, std::size_t Offset, std::size_t Count>
  357. struct calculate_subspan_type
  358. {
  359. using type = span<ElementType, Count != dynamic_extent
  360. ? Count
  361. : (Extent != dynamic_extent ? Extent - Offset : Extent)>;
  362. };
  363. } // namespace details
  364. // [span], class template span
  365. template <class ElementType, std::size_t Extent>
  366. class span
  367. {
  368. public:
  369. // constants and types
  370. using element_type = ElementType;
  371. using value_type = std::remove_cv_t<ElementType>;
  372. using size_type = std::size_t;
  373. using pointer = element_type*;
  374. using const_pointer = const element_type*;
  375. using reference = element_type&;
  376. using const_reference = const element_type&;
  377. using difference_type = std::ptrdiff_t;
  378. using iterator = details::span_iterator<ElementType>;
  379. using reverse_iterator = std::reverse_iterator<iterator>;
  380. #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
  381. static constexpr const size_type extent{Extent};
  382. #else
  383. static constexpr size_type extent{Extent};
  384. #endif
  385. // [span.cons], span constructors, copy, assignment, and destructor
  386. template <bool Dependent = false,
  387. // "Dependent" is needed to make "std::enable_if_t<Dependent || Extent == 0 || Extent
  388. // == dynamic_extent>" SFINAE, since "std::enable_if_t<Extent == 0 || Extent ==
  389. // dynamic_extent>" is ill-formed when Extent is greater than 0.
  390. class = std::enable_if_t<(Dependent ||
  391. details::is_allowed_extent_conversion<0, Extent>::value)>>
  392. constexpr span() noexcept : storage_(nullptr, details::extent_type<0>())
  393. {}
  394. template <std::size_t MyExtent = Extent, std::enable_if_t<MyExtent != dynamic_extent, int> = 0>
  395. constexpr explicit span(pointer ptr, size_type count) noexcept : storage_(ptr, count)
  396. {
  397. Expects(count == Extent);
  398. }
  399. template <std::size_t MyExtent = Extent, std::enable_if_t<MyExtent == dynamic_extent, int> = 0>
  400. constexpr span(pointer ptr, size_type count) noexcept : storage_(ptr, count)
  401. {}
  402. template <std::size_t MyExtent = Extent, std::enable_if_t<MyExtent != dynamic_extent, int> = 0>
  403. constexpr explicit span(pointer firstElem, pointer lastElem) noexcept
  404. : storage_(firstElem, narrow_cast<std::size_t>(lastElem - firstElem))
  405. {
  406. Expects(lastElem - firstElem == static_cast<difference_type>(Extent));
  407. }
  408. template <std::size_t MyExtent = Extent, std::enable_if_t<MyExtent == dynamic_extent, int> = 0>
  409. constexpr span(pointer firstElem, pointer lastElem) noexcept
  410. : storage_(firstElem, narrow_cast<std::size_t>(lastElem - firstElem))
  411. {}
  412. template <std::size_t N,
  413. std::enable_if_t<details::is_allowed_extent_conversion<N, Extent>::value, int> = 0>
  414. constexpr span(element_type (&arr)[N]) noexcept
  415. : storage_(KnownNotNull{arr}, details::extent_type<N>())
  416. {}
  417. template <
  418. class T, std::size_t N,
  419. std::enable_if_t<(details::is_allowed_extent_conversion<N, Extent>::value &&
  420. details::is_allowed_element_type_conversion<T, element_type>::value),
  421. int> = 0>
  422. constexpr span(std::array<T, N>& arr) noexcept
  423. : storage_(KnownNotNull{arr.data()}, details::extent_type<N>())
  424. {}
  425. template <class T, std::size_t N,
  426. std::enable_if_t<
  427. (details::is_allowed_extent_conversion<N, Extent>::value &&
  428. details::is_allowed_element_type_conversion<const T, element_type>::value),
  429. int> = 0>
  430. constexpr span(const std::array<T, N>& arr) noexcept
  431. : storage_(KnownNotNull{arr.data()}, details::extent_type<N>())
  432. {}
  433. // NB: the SFINAE on these constructors uses .data() as an incomplete/imperfect proxy for the
  434. // requirement on Container to be a contiguous sequence container.
  435. template <std::size_t MyExtent = Extent, class Container,
  436. std::enable_if_t<
  437. MyExtent != dynamic_extent && !details::is_span<Container>::value &&
  438. !details::is_std_array<Container>::value &&
  439. std::is_pointer<decltype(std::declval<Container&>().data())>::value &&
  440. std::is_convertible<
  441. std::remove_pointer_t<decltype(std::declval<Container&>().data())> (*)[],
  442. element_type (*)[]>::value,
  443. int> = 0>
  444. constexpr explicit span(Container& cont) noexcept : span(cont.data(), cont.size())
  445. {}
  446. template <std::size_t MyExtent = Extent, class Container,
  447. std::enable_if_t<
  448. MyExtent == dynamic_extent && !details::is_span<Container>::value &&
  449. !details::is_std_array<Container>::value &&
  450. std::is_pointer<decltype(std::declval<Container&>().data())>::value &&
  451. std::is_convertible<
  452. std::remove_pointer_t<decltype(std::declval<Container&>().data())> (*)[],
  453. element_type (*)[]>::value,
  454. int> = 0>
  455. constexpr span(Container& cont) noexcept : span(cont.data(), cont.size())
  456. {}
  457. template <
  458. std::size_t MyExtent = Extent, class Container,
  459. std::enable_if_t<
  460. MyExtent != dynamic_extent && std::is_const<element_type>::value &&
  461. !details::is_span<Container>::value && !details::is_std_array<Container>::value &&
  462. std::is_pointer<decltype(std::declval<const Container&>().data())>::value &&
  463. std::is_convertible<
  464. std::remove_pointer_t<decltype(std::declval<const Container&>().data())> (*)[],
  465. element_type (*)[]>::value,
  466. int> = 0>
  467. constexpr explicit span(const Container& cont) noexcept : span(cont.data(), cont.size())
  468. {}
  469. template <
  470. std::size_t MyExtent = Extent, class Container,
  471. std::enable_if_t<
  472. MyExtent == dynamic_extent && std::is_const<element_type>::value &&
  473. !details::is_span<Container>::value && !details::is_std_array<Container>::value &&
  474. std::is_pointer<decltype(std::declval<const Container&>().data())>::value &&
  475. std::is_convertible<
  476. std::remove_pointer_t<decltype(std::declval<const Container&>().data())> (*)[],
  477. element_type (*)[]>::value,
  478. int> = 0>
  479. constexpr span(const Container& cont) noexcept : span(cont.data(), cont.size())
  480. {}
  481. constexpr span(const span& other) noexcept = default;
  482. template <class OtherElementType, std::size_t OtherExtent, std::size_t MyExtent = Extent,
  483. std::enable_if_t<(MyExtent == dynamic_extent || MyExtent == OtherExtent) &&
  484. details::is_allowed_element_type_conversion<OtherElementType,
  485. element_type>::value,
  486. int> = 0>
  487. constexpr span(const span<OtherElementType, OtherExtent>& other) noexcept
  488. : storage_(other.data(), details::extent_type<OtherExtent>(other.size()))
  489. {}
  490. template <class OtherElementType, std::size_t OtherExtent, std::size_t MyExtent = Extent,
  491. std::enable_if_t<MyExtent != dynamic_extent && OtherExtent == dynamic_extent &&
  492. details::is_allowed_element_type_conversion<OtherElementType,
  493. element_type>::value,
  494. int> = 0>
  495. constexpr explicit span(const span<OtherElementType, OtherExtent>& other) noexcept
  496. : storage_(other.data(), details::extent_type<OtherExtent>(other.size()))
  497. {}
  498. ~span() noexcept = default;
  499. constexpr span& operator=(const span& other) noexcept = default;
  500. // [span.sub], span subviews
  501. template <std::size_t Count>
  502. constexpr span<element_type, Count> first() const noexcept
  503. {
  504. Expects(Count <= size());
  505. return span<element_type, Count>{data(), Count};
  506. }
  507. template <std::size_t Count>
  508. // clang-format off
  509. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  510. // clang-format on
  511. constexpr span<element_type, Count> last() const noexcept
  512. {
  513. Expects(Count <= size());
  514. return span<element_type, Count>{data() + (size() - Count), Count};
  515. }
  516. template <std::size_t Offset, std::size_t Count = dynamic_extent>
  517. // clang-format off
  518. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  519. // clang-format on
  520. constexpr auto subspan() const noexcept ->
  521. typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type
  522. {
  523. Expects((size() >= Offset) && (Count == dynamic_extent || (Count <= size() - Offset)));
  524. using type =
  525. typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type;
  526. return type{data() + Offset, Count == dynamic_extent ? size() - Offset : Count};
  527. }
  528. constexpr span<element_type, dynamic_extent> first(size_type count) const noexcept
  529. {
  530. Expects(count <= size());
  531. return {data(), count};
  532. }
  533. constexpr span<element_type, dynamic_extent> last(size_type count) const noexcept
  534. {
  535. Expects(count <= size());
  536. return make_subspan(size() - count, dynamic_extent, subspan_selector<Extent>{});
  537. }
  538. constexpr span<element_type, dynamic_extent>
  539. subspan(size_type offset, size_type count = dynamic_extent) const noexcept
  540. {
  541. return make_subspan(offset, count, subspan_selector<Extent>{});
  542. }
  543. // [span.obs], span observers
  544. constexpr size_type size() const noexcept { return storage_.size(); }
  545. constexpr size_type size_bytes() const noexcept { return size() * sizeof(element_type); }
  546. constexpr bool empty() const noexcept { return size() == 0; }
  547. // [span.elem], span element access
  548. // clang-format off
  549. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  550. // clang-format on
  551. constexpr reference operator[](size_type idx) const noexcept
  552. {
  553. Expects(idx < size());
  554. return data()[idx];
  555. }
  556. constexpr reference front() const noexcept
  557. {
  558. Expects(size() > 0);
  559. return data()[0];
  560. }
  561. constexpr reference back() const noexcept
  562. {
  563. Expects(size() > 0);
  564. return data()[size() - 1];
  565. }
  566. constexpr pointer data() const noexcept { return storage_.data(); }
  567. // [span.iter], span iterator support
  568. constexpr iterator begin() const noexcept
  569. {
  570. const auto data = storage_.data();
  571. // clang-format off
  572. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  573. // clang-format on
  574. return {data, data + size(), data};
  575. }
  576. constexpr iterator end() const noexcept
  577. {
  578. const auto data = storage_.data();
  579. // clang-format off
  580. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  581. // clang-format on
  582. const auto endData = data + storage_.size();
  583. return {data, endData, endData};
  584. }
  585. constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; }
  586. constexpr reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; }
  587. #ifdef _MSC_VER
  588. // Tell MSVC how to unwrap spans in range-based-for
  589. constexpr pointer _Unchecked_begin() const noexcept { return data(); }
  590. constexpr pointer _Unchecked_end() const noexcept
  591. {
  592. // clang-format off
  593. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  594. // clang-format on
  595. return data() + size();
  596. }
  597. #endif // _MSC_VER
  598. private:
  599. // Needed to remove unnecessary null check in subspans
  600. struct KnownNotNull
  601. {
  602. pointer p;
  603. };
  604. // this implementation detail class lets us take advantage of the
  605. // empty base class optimization to pay for only storage of a single
  606. // pointer in the case of fixed-size spans
  607. template <class ExtentType>
  608. class storage_type : public ExtentType
  609. {
  610. public:
  611. // KnownNotNull parameter is needed to remove unnecessary null check
  612. // in subspans and constructors from arrays
  613. template <class OtherExtentType>
  614. constexpr storage_type(KnownNotNull data, OtherExtentType ext)
  615. : ExtentType(ext), data_(data.p)
  616. {}
  617. template <class OtherExtentType>
  618. constexpr storage_type(pointer data, OtherExtentType ext) : ExtentType(ext), data_(data)
  619. {
  620. Expects(data || ExtentType::size() == 0);
  621. }
  622. constexpr pointer data() const noexcept { return data_; }
  623. private:
  624. pointer data_;
  625. };
  626. storage_type<details::extent_type<Extent>> storage_;
  627. // The rest is needed to remove unnecessary null check
  628. // in subspans and constructors from arrays
  629. constexpr span(KnownNotNull ptr, size_type count) noexcept : storage_(ptr, count) {}
  630. template <std::size_t CallerExtent>
  631. class subspan_selector
  632. {
  633. };
  634. template <std::size_t CallerExtent>
  635. constexpr span<element_type, dynamic_extent>
  636. make_subspan(size_type offset, size_type count, subspan_selector<CallerExtent>) const noexcept
  637. {
  638. const span<element_type, dynamic_extent> tmp(*this);
  639. return tmp.subspan(offset, count);
  640. }
  641. // clang-format off
  642. GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
  643. // clang-format on
  644. constexpr span<element_type, dynamic_extent>
  645. make_subspan(size_type offset, size_type count, subspan_selector<dynamic_extent>) const noexcept
  646. {
  647. Expects(size() >= offset);
  648. if (count == dynamic_extent) { return {KnownNotNull{data() + offset}, size() - offset}; }
  649. Expects(size() - offset >= count);
  650. return {KnownNotNull{data() + offset}, count};
  651. }
  652. };
  653. #if (defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L))
  654. // Deduction Guides
  655. template <class Type, std::size_t Extent>
  656. span(Type (&)[Extent]) -> span<Type, Extent>;
  657. template <class Type, std::size_t Size>
  658. span(std::array<Type, Size>&) -> span<Type, Size>;
  659. template <class Type, std::size_t Size>
  660. span(const std::array<Type, Size>&) -> span<const Type, Size>;
  661. template <class Container,
  662. class Element = std::remove_pointer_t<decltype(std::declval<Container&>().data())>>
  663. span(Container&) -> span<Element>;
  664. template <class Container,
  665. class Element = std::remove_pointer_t<decltype(std::declval<const Container&>().data())>>
  666. span(const Container&) -> span<Element>;
  667. #endif // ( defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L) )
  668. #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
  669. #if defined(__clang__) && defined(_MSC_VER) && defined(__cplusplus) && (__cplusplus < 201703L)
  670. #pragma clang diagnostic push
  671. #pragma clang diagnostic ignored "-Wdeprecated" // Bug in clang-cl.exe which raises a C++17 -Wdeprecated warning about this static constexpr workaround in C++14 mode.
  672. #endif // defined(__clang__) && defined(_MSC_VER) && defined(__cplusplus) && (__cplusplus < 201703L)
  673. template <class ElementType, std::size_t Extent>
  674. constexpr const typename span<ElementType, Extent>::size_type span<ElementType, Extent>::extent;
  675. #if defined(__clang__) && defined(_MSC_VER) && defined(__cplusplus) && (__cplusplus < 201703L)
  676. #pragma clang diagnostic pop
  677. #endif // defined(__clang__) && defined(_MSC_VER) && defined(__cplusplus) && (__cplusplus < 201703L)
  678. #endif
  679. namespace details
  680. {
  681. // if we only supported compilers with good constexpr support then
  682. // this pair of classes could collapse down to a constexpr function
  683. // we should use a narrow_cast<> to go to std::size_t, but older compilers may not see it as
  684. // constexpr
  685. // and so will fail compilation of the template
  686. template <class ElementType, std::size_t Extent>
  687. struct calculate_byte_size : std::integral_constant<std::size_t, sizeof(ElementType) * Extent>
  688. {
  689. static_assert(Extent < dynamic_extent / sizeof(ElementType), "Size is too big.");
  690. };
  691. template <class ElementType>
  692. struct calculate_byte_size<ElementType, dynamic_extent>
  693. : std::integral_constant<std::size_t, dynamic_extent>
  694. {
  695. };
  696. } // namespace details
  697. // [span.objectrep], views of object representation
  698. template <class ElementType, std::size_t Extent>
  699. span<const byte, details::calculate_byte_size<ElementType, Extent>::value>
  700. as_bytes(span<ElementType, Extent> s) noexcept
  701. {
  702. using type = span<const byte, details::calculate_byte_size<ElementType, Extent>::value>;
  703. // clang-format off
  704. GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
  705. // clang-format on
  706. return type{reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
  707. }
  708. template <class ElementType, std::size_t Extent,
  709. std::enable_if_t<!std::is_const<ElementType>::value, int> = 0>
  710. span<byte, details::calculate_byte_size<ElementType, Extent>::value>
  711. as_writable_bytes(span<ElementType, Extent> s) noexcept
  712. {
  713. using type = span<byte, details::calculate_byte_size<ElementType, Extent>::value>;
  714. // clang-format off
  715. GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
  716. // clang-format on
  717. return type{reinterpret_cast<byte*>(s.data()), s.size_bytes()};
  718. }
  719. } // namespace gsl
  720. #if defined(_MSC_VER) && !defined(__clang__)
  721. #pragma warning(pop)
  722. #endif // _MSC_VER
  723. #if defined(__GNUC__) && __GNUC__ > 6
  724. #pragma GCC diagnostic pop
  725. #endif // __GNUC__ > 6
  726. #if defined(__clang__)
  727. #if __has_warning("-Wunsafe-buffer-usage")
  728. #pragma clang diagnostic pop
  729. #endif // __has_warning("-Wunsafe-buffer-usage")
  730. #endif // defined(__clang__)
  731. #endif // GSL_SPAN_H