GSL/include/gsl/span

881 lines
29 KiB
Plaintext
Raw Normal View History

2016-02-24 14:26:28 -05:00
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef GSL_SPAN_H
#define GSL_SPAN_H
#include <gsl/gsl_assert> // for Expects
#include <gsl/gsl_byte> // for byte
#include <gsl/gsl_util> // for narrow_cast, narrow
#include <algorithm> // for lexicographical_compare
#include <array> // for array
#include <cstddef> // for ptrdiff_t, size_t, nullptr_t
#include <iterator> // for reverse_iterator, distance, random_access_...
#include <memory> // for std::addressof
2016-02-24 14:26:28 -05:00
#include <stdexcept>
#include <type_traits> // for enable_if_t, declval, is_convertible, inte...
2016-02-24 14:26:28 -05:00
#include <utility>
Clang cl (#762) * Added c++17 test configurations for clang5.0 and clang6.0 * Fix #739 correct cppcorecheck warnings for clang-cl * Add clang-cl configurations * Corrections Appveyor; Temporarily disable msvc compilation for faster testing (#741) * Add path to clang-cl.exe (#741) * Escape backslash in path (#741) * Update vcpkg (#741) * Check vcpkg version; try without building vcpkg; use latest clang-cl from path (#741) * Fix blocks in ps script (#741) * Try accessing APPVEYOR_BUILD_FOLDER variable (#471) * Update span size() bug confirmation test for GCC 6.5 (#741) * MSVC flags to Clang-cl; disable c++98-compat and undefined macro warnings (#741) * Suppress clang warning on tests (missing-prototypes) (#741) * Fix clang warning -Wnewline-eof (#741) * Fix clang warning -Wdouble-promotion (#741) * Set linker explicitly * Clean condition statement * For Clang, fallback to the AppVeyor installed version of CMake * Fix clang warning -Wmissing-variable-declarations * Fallback to the MSVC linker until vcpkg has been updated * Revert "Fallback to the MSVC linker until vcpkg has been updated" This reverts commit 7263f3289e6e835c32fc7800d0c2d4e58bd21178. * Fix clang warning -Wunused-member-function * Fix clang warning -Wmissing-noreturn * Fix clang warning -Winvalid-noreturn on Windows * Add macro block end comment on large #if blocks * Workaround: fallback to mscv link.exe * Workaround: get msvc paths into PowerShell through intermediate file * Workaround: fix, remove "PATH=" from text * Workaround: try with full-path; and return user PATH * Workaround: fix, escape backslashes * Revert all since "Workaround: fallback to mscv link.exe" did not work on AppVeyor This reverts the commits: bda3d6a428e5d19e97375b2e575b0f51ff1b4bc0 97062933acfb6428535c0fdcab5d94371ce72bfc 0f4fb04bac9c2d091005b791294237250dbe0668 1b0c19afd154f4ffc5ef793014c1bc2324534fd0 a5739ea5f0cd7633527b5e5d3b16a9e3ade7149f * Suppress output of git pull; remove vcpkg from cache * Re-enable AppVeyor builds for all platforms * Correct typo Co-Authored-By: Farwaykorse <Farwaykorse@users.noreply.github.com> * Add Clang-cl 7.0.0 to the supported platforms * Revert "Fix clang warning -Wunused-member-function" This reverts commit 6fe1a4203501145ab4350b9152ecc11f3a30e49a. * Fix or locally suppress clang warning -Wunused-member-function * format touched code and correct comment * git pull --quiet * fix logic error in workaround * fix missing bracket * Suppress output of mkdir * Replace MSBuild with Ninja * Suppress output of 7z * Add architecture flags for Clang * Drop workaround for lld-link * 7-zip Overwrite and Alternative output suppression without suppressing errors Replaces 3c1c0794dd9a29908cc7c38f591bd3c7b4929677 * AppVeyor setup and CMake before build * reorder compiler configuration * remove unnecessary * remove -fno-strict-aliasing * remove -Wsign-conversion, since before v4.0 part of -Wconversion * -Wctor-dtor-privacy is GCC only * remove -Woverloaded-virtual part of -Wmost, part of -Wall * add -Wmissing-noreturn * remove the pragmas for -Wunused-member-function * Re-add MSBuild generator on AppVeyor * Print CMake commands * Add MSBuild toolset selection * Separate Architecture setting * clang-cl: add -Weverything * clang-cl -Wno-c++98-compat * clang-cl -Wno-c++98-compat-pedantic * clang-cl -Wno-missing-prototypes * clang-cl C++14 -Wno-unused-member-function * clang-cl -Wundef __GNUC__ * clang++: add -Weverything * clang++ -Wno-c++98-compat * clang++ -Wno-c++98-compat-pedantic * clang++ -Wno-missing-prototypes * clang++ -Wno-weak-vtables * clang++ C++14 -Wno-unused-member-function * clang++ fix -Wundef _MSC_VER * clang++ -Wno-padded * clang++ solve -Wdeprecated * Add AppleClang compiler target Since CMake v3.0 use of Clang for both is deprecated * clang++ v5.0 C++17 -Wno-undefined-func-template * Add VS2015 + LLVM/clang-cl to AppVeyor * Do not disable constexpr when compiling with clang-cl on Windows * Clean-up clang-only warnings (now under -Weverything) * Revert "Fix clang warning -Winvalid-noreturn on Windows" This reverts commit 2238c4760e86feebb2a18620b77312bd01055f61. * Suppress -Winvalid-noreturn for the MS STL noexception workaround * CMake: put preprocessor definition in target_compile_definitions * Solve compiler warning C4668: __GNUC__ not defined
2019-01-15 13:27:34 -05:00
#if defined(_MSC_VER) && !defined(__clang__)
#pragma warning(push)
// turn off some warnings that are noisy about our Expects statements
#pragma warning(disable : 4127) // conditional expression is constant
#pragma warning( \
disable : 4146) // unary minus operator applied to unsigned type, result still unsigned
#pragma warning(disable : 4702) // unreachable code
// Turn MSVC /analyze rules that generate too much noise. TODO: fix in the tool.
#pragma warning(disable : 26495) // uninitalized member when constructor calls constructor
#pragma warning(disable : 26446) // parser bug does not allow attributes on some templates
#if _MSC_VER < 1910
#pragma push_macro("constexpr")
#define constexpr /*constexpr*/
#define GSL_USE_STATIC_CONSTEXPR_WORKAROUND
#endif // _MSC_VER < 1910
#endif // _MSC_VER
// See if we have enough C++17 power to use a static constexpr data member
// without needing an out-of-line definition
#if !(defined(__cplusplus) && (__cplusplus >= 201703L))
#define GSL_USE_STATIC_CONSTEXPR_WORKAROUND
#endif // !(defined(__cplusplus) && (__cplusplus >= 201703L))
// GCC 7 does not like the signed unsigned missmatch (size_t ptrdiff_t)
// While there is a conversion from signed to unsigned, it happens at
2020-01-17 00:32:37 -05:00
// compiletime, so the compiler wouldn't have to warn indiscriminately, but
// could check if the source value actually doesn't fit into the target type
// and only warn in those cases.
Clang cl (#762) * Added c++17 test configurations for clang5.0 and clang6.0 * Fix #739 correct cppcorecheck warnings for clang-cl * Add clang-cl configurations * Corrections Appveyor; Temporarily disable msvc compilation for faster testing (#741) * Add path to clang-cl.exe (#741) * Escape backslash in path (#741) * Update vcpkg (#741) * Check vcpkg version; try without building vcpkg; use latest clang-cl from path (#741) * Fix blocks in ps script (#741) * Try accessing APPVEYOR_BUILD_FOLDER variable (#471) * Update span size() bug confirmation test for GCC 6.5 (#741) * MSVC flags to Clang-cl; disable c++98-compat and undefined macro warnings (#741) * Suppress clang warning on tests (missing-prototypes) (#741) * Fix clang warning -Wnewline-eof (#741) * Fix clang warning -Wdouble-promotion (#741) * Set linker explicitly * Clean condition statement * For Clang, fallback to the AppVeyor installed version of CMake * Fix clang warning -Wmissing-variable-declarations * Fallback to the MSVC linker until vcpkg has been updated * Revert "Fallback to the MSVC linker until vcpkg has been updated" This reverts commit 7263f3289e6e835c32fc7800d0c2d4e58bd21178. * Fix clang warning -Wunused-member-function * Fix clang warning -Wmissing-noreturn * Fix clang warning -Winvalid-noreturn on Windows * Add macro block end comment on large #if blocks * Workaround: fallback to mscv link.exe * Workaround: get msvc paths into PowerShell through intermediate file * Workaround: fix, remove "PATH=" from text * Workaround: try with full-path; and return user PATH * Workaround: fix, escape backslashes * Revert all since "Workaround: fallback to mscv link.exe" did not work on AppVeyor This reverts the commits: bda3d6a428e5d19e97375b2e575b0f51ff1b4bc0 97062933acfb6428535c0fdcab5d94371ce72bfc 0f4fb04bac9c2d091005b791294237250dbe0668 1b0c19afd154f4ffc5ef793014c1bc2324534fd0 a5739ea5f0cd7633527b5e5d3b16a9e3ade7149f * Suppress output of git pull; remove vcpkg from cache * Re-enable AppVeyor builds for all platforms * Correct typo Co-Authored-By: Farwaykorse <Farwaykorse@users.noreply.github.com> * Add Clang-cl 7.0.0 to the supported platforms * Revert "Fix clang warning -Wunused-member-function" This reverts commit 6fe1a4203501145ab4350b9152ecc11f3a30e49a. * Fix or locally suppress clang warning -Wunused-member-function * format touched code and correct comment * git pull --quiet * fix logic error in workaround * fix missing bracket * Suppress output of mkdir * Replace MSBuild with Ninja * Suppress output of 7z * Add architecture flags for Clang * Drop workaround for lld-link * 7-zip Overwrite and Alternative output suppression without suppressing errors Replaces 3c1c0794dd9a29908cc7c38f591bd3c7b4929677 * AppVeyor setup and CMake before build * reorder compiler configuration * remove unnecessary * remove -fno-strict-aliasing * remove -Wsign-conversion, since before v4.0 part of -Wconversion * -Wctor-dtor-privacy is GCC only * remove -Woverloaded-virtual part of -Wmost, part of -Wall * add -Wmissing-noreturn * remove the pragmas for -Wunused-member-function * Re-add MSBuild generator on AppVeyor * Print CMake commands * Add MSBuild toolset selection * Separate Architecture setting * clang-cl: add -Weverything * clang-cl -Wno-c++98-compat * clang-cl -Wno-c++98-compat-pedantic * clang-cl -Wno-missing-prototypes * clang-cl C++14 -Wno-unused-member-function * clang-cl -Wundef __GNUC__ * clang++: add -Weverything * clang++ -Wno-c++98-compat * clang++ -Wno-c++98-compat-pedantic * clang++ -Wno-missing-prototypes * clang++ -Wno-weak-vtables * clang++ C++14 -Wno-unused-member-function * clang++ fix -Wundef _MSC_VER * clang++ -Wno-padded * clang++ solve -Wdeprecated * Add AppleClang compiler target Since CMake v3.0 use of Clang for both is deprecated * clang++ v5.0 C++17 -Wno-undefined-func-template * Add VS2015 + LLVM/clang-cl to AppVeyor * Do not disable constexpr when compiling with clang-cl on Windows * Clean-up clang-only warnings (now under -Weverything) * Revert "Fix clang warning -Winvalid-noreturn on Windows" This reverts commit 2238c4760e86feebb2a18620b77312bd01055f61. * Suppress -Winvalid-noreturn for the MS STL noexception workaround * CMake: put preprocessor definition in target_compile_definitions * Solve compiler warning C4668: __GNUC__ not defined
2019-01-15 13:27:34 -05:00
#if defined(__GNUC__) && __GNUC__ > 6
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion"
#endif
2016-02-24 14:26:28 -05:00
namespace gsl
{
2016-07-20 16:17:47 -04:00
// [views.constants], constants
2020-02-03 15:49:57 -05:00
constexpr const std::size_t dynamic_extent = static_cast<std::size_t>(-1);
template <class ElementType, std::size_t Extent = dynamic_extent>
class span;
// implementation details
2016-03-01 15:11:41 -05:00
namespace details
{
2016-07-20 16:17:47 -04:00
template <class T>
struct is_span_oracle : std::false_type
{
};
2016-03-01 15:11:41 -05:00
template <class ElementType, std::size_t Extent>
2016-07-20 16:17:47 -04:00
struct is_span_oracle<gsl::span<ElementType, Extent>> : std::true_type
{
};
2016-03-16 22:39:55 -04:00
2016-07-20 16:17:47 -04:00
template <class T>
struct is_span : public is_span_oracle<std::remove_cv_t<T>>
{
};
template <class T>
struct is_std_array_oracle : std::false_type
{
};
2017-02-13 15:11:45 -05:00
template <class ElementType, std::size_t Extent>
struct is_std_array_oracle<std::array<ElementType, Extent>> : std::true_type
{
};
template <class T>
struct is_std_array : public is_std_array_oracle<std::remove_cv_t<T>>
2016-07-20 16:17:47 -04:00
{
};
2016-03-16 22:39:55 -04:00
template <std::size_t From, std::size_t To>
2016-07-20 16:17:47 -04:00
struct is_allowed_extent_conversion
2016-07-26 22:19:47 -04:00
: public std::integral_constant<bool, From == To || From == gsl::dynamic_extent ||
To == gsl::dynamic_extent>
2016-07-20 16:17:47 -04:00
{
};
2016-03-16 22:39:55 -04:00
2016-07-20 16:17:47 -04:00
template <class From, class To>
struct is_allowed_element_type_conversion
: public std::integral_constant<bool, std::is_convertible<From (*)[], To (*)[]>::value>
2016-07-20 16:17:47 -04:00
{
};
template <class Span, bool IsConst>
2016-08-02 00:41:20 -04:00
class span_iterator
2016-07-20 16:17:47 -04:00
{
using element_type_ = typename Span::element_type;
2016-07-20 16:17:47 -04:00
public:
#ifdef _MSC_VER
// Tell Microsoft standard library that span_iterators are checked.
using _Unchecked_type = typename Span::pointer;
#endif
2016-07-20 16:17:47 -04:00
using iterator_category = std::random_access_iterator_tag;
using value_type = std::remove_cv_t<element_type_>;
using difference_type = typename Span::index_type;
using reference = std::conditional_t<IsConst, const element_type_, element_type_>&;
using pointer = std::add_pointer_t<reference>;
span_iterator() = default;
constexpr span_iterator(const Span* span, difference_type idx) noexcept
: span_(span), index_(idx)
{}
friend span_iterator<Span, true>;
template <bool B, std::enable_if_t<!B && IsConst>* = nullptr>
constexpr span_iterator(const span_iterator<Span, B>& other) noexcept
: span_iterator(other.span_, other.index_)
{}
2020-02-03 16:52:00 -05:00
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
constexpr reference operator*() const
2016-07-20 16:17:47 -04:00
{
Expects(static_cast<size_t>(index_) != span_->size());
return *(span_->data() + index_);
2016-07-20 16:17:47 -04:00
}
constexpr pointer operator->() const
2016-07-20 16:17:47 -04:00
{
Expects(index_ != span_->size());
return span_->data() + index_;
2016-07-20 16:17:47 -04:00
}
constexpr span_iterator& operator++() noexcept
2016-07-20 16:17:47 -04:00
{
Expects(0 <= index_ && static_cast<size_t>(index_) != span_->size());
++index_;
2016-07-20 16:17:47 -04:00
return *this;
}
constexpr span_iterator operator++(int) noexcept
{
auto ret = *this;
++(*this);
return ret;
}
constexpr span_iterator& operator--() noexcept
2016-07-20 16:17:47 -04:00
{
Expects(index_ != 0 && static_cast<size_t>(index_) <= span_->size());
--index_;
2016-07-20 16:17:47 -04:00
return *this;
}
constexpr span_iterator operator--(int) noexcept
{
auto ret = *this;
--(*this);
return ret;
}
constexpr span_iterator operator+(difference_type n) const noexcept
2016-07-20 16:17:47 -04:00
{
auto ret = *this;
return ret += n;
2016-07-20 16:17:47 -04:00
}
friend constexpr span_iterator operator+(difference_type n,
span_iterator const& rhs) noexcept
{
return rhs + n;
}
constexpr span_iterator& operator+=(difference_type n) noexcept
2016-07-20 16:17:47 -04:00
{
Expects((index_ + n) >= 0 && (index_ + n) <= span_->size());
index_ += n;
return *this;
2016-07-20 16:17:47 -04:00
}
constexpr span_iterator operator-(difference_type n) const noexcept
2016-07-20 16:17:47 -04:00
{
auto ret = *this;
return ret -= n;
2016-07-20 16:17:47 -04:00
}
constexpr span_iterator& operator-=(difference_type n) noexcept { return *this += -n; }
constexpr difference_type operator-(span_iterator rhs) const noexcept
2016-07-20 16:17:47 -04:00
{
Expects(span_ == rhs.span_);
return index_ - rhs.index_;
2016-07-20 16:17:47 -04:00
}
constexpr reference operator[](difference_type n) const noexcept { return *(*this + n); }
constexpr friend bool operator==(span_iterator lhs, span_iterator rhs) noexcept
2016-07-20 16:17:47 -04:00
{
return lhs.span_ == rhs.span_ && lhs.index_ == rhs.index_;
2016-07-20 16:17:47 -04:00
}
constexpr friend bool operator!=(span_iterator lhs, span_iterator rhs) noexcept
2016-07-20 16:17:47 -04:00
{
return !(lhs == rhs);
2016-07-20 16:17:47 -04:00
}
constexpr friend bool operator<(span_iterator lhs, span_iterator rhs) noexcept
2016-07-20 16:17:47 -04:00
{
return lhs.index_ < rhs.index_;
2016-07-20 16:17:47 -04:00
}
constexpr friend bool operator<=(span_iterator lhs, span_iterator rhs) noexcept
2016-07-20 16:17:47 -04:00
{
return !(rhs < lhs);
2016-07-20 16:17:47 -04:00
}
constexpr friend bool operator>(span_iterator lhs, span_iterator rhs) noexcept
{
return rhs < lhs;
}
constexpr friend bool operator>=(span_iterator lhs, span_iterator rhs) noexcept
2016-07-20 16:17:47 -04:00
{
return !(rhs > lhs);
2016-07-20 16:17:47 -04:00
}
Add usage for check-and-unwrap of MS STL iterators (#687) * Add usage for check-and-unwrap of MS STL iterators This is Billy ONeal's PR #682 with a typo fixed. See corresponding change here: https://devdiv.visualstudio.com/DefaultCollection/DevDiv/_git/msvc/commit/ca77129308c775f422ecef0c01ad460f75c462ad That change officially exposes the STL's range checking machinery, available in MSVC++ "15.8"+ This change augments GSL span::iterator to call into that newly exposed machinery. _Verify_range(cit, cit) Requests that the iterator type check that the parameters form a valid [First, Last) range iterator pair. This replaces _DEBUG_RANGE and supporting machinery. The standard library provides a version for pointers under _IDL != 0; otherwise they are normally provided via hidden friend functions. Note that declaring some of these hidden friends for "wrapper" iterators like move_iterator and reverse_iterator triggers VSO#610735. cit._Verify_offset(difference_type _Off) For random-access iterators, performs any asserts that would be performed by i += _Off; except with possibly a better error message and without moving the iterator. cit._Unwrapped() Returns an "unchecked" or "unwrapped" iterator which has previously been validated. The iterator may have been validated by a call to _Verify_range or _Verify_offset (above), or by seeking a checked iterator to a "high water mark" point. it._Seek_to(cit) / it._Seek_to(return value from _Unwrapped()) Moves the position of the checked iterator it to the position of the unchecked iterator supplied. Generally not intended to perform range checks. * Fixed build break in VS2015 * fixed constexpr build break when using VS2015
2018-05-22 21:07:49 -04:00
#ifdef _MSC_VER
// MSVC++ iterator debugging support; allows STL algorithms in 15.8+
// to unwrap span_iterator to a pointer type after a range check in STL
// algorithm calls
friend constexpr void _Verify_range(span_iterator lhs, span_iterator rhs) noexcept
{ // test that [lhs, rhs) forms a valid range inside an STL algorithm
Expects(lhs.span_ == rhs.span_ // range spans have to match
&& lhs.index_ <= rhs.index_); // range must not be transposed
Add usage for check-and-unwrap of MS STL iterators (#687) * Add usage for check-and-unwrap of MS STL iterators This is Billy ONeal's PR #682 with a typo fixed. See corresponding change here: https://devdiv.visualstudio.com/DefaultCollection/DevDiv/_git/msvc/commit/ca77129308c775f422ecef0c01ad460f75c462ad That change officially exposes the STL's range checking machinery, available in MSVC++ "15.8"+ This change augments GSL span::iterator to call into that newly exposed machinery. _Verify_range(cit, cit) Requests that the iterator type check that the parameters form a valid [First, Last) range iterator pair. This replaces _DEBUG_RANGE and supporting machinery. The standard library provides a version for pointers under _IDL != 0; otherwise they are normally provided via hidden friend functions. Note that declaring some of these hidden friends for "wrapper" iterators like move_iterator and reverse_iterator triggers VSO#610735. cit._Verify_offset(difference_type _Off) For random-access iterators, performs any asserts that would be performed by i += _Off; except with possibly a better error message and without moving the iterator. cit._Unwrapped() Returns an "unchecked" or "unwrapped" iterator which has previously been validated. The iterator may have been validated by a call to _Verify_range or _Verify_offset (above), or by seeking a checked iterator to a "high water mark" point. it._Seek_to(cit) / it._Seek_to(return value from _Unwrapped()) Moves the position of the checked iterator it to the position of the unchecked iterator supplied. Generally not intended to perform range checks. * Fixed build break in VS2015 * fixed constexpr build break when using VS2015
2018-05-22 21:07:49 -04:00
}
constexpr void _Verify_offset(const difference_type n) const noexcept
{ // test that the iterator *this + n is a valid range in an STL
Add usage for check-and-unwrap of MS STL iterators (#687) * Add usage for check-and-unwrap of MS STL iterators This is Billy ONeal's PR #682 with a typo fixed. See corresponding change here: https://devdiv.visualstudio.com/DefaultCollection/DevDiv/_git/msvc/commit/ca77129308c775f422ecef0c01ad460f75c462ad That change officially exposes the STL's range checking machinery, available in MSVC++ "15.8"+ This change augments GSL span::iterator to call into that newly exposed machinery. _Verify_range(cit, cit) Requests that the iterator type check that the parameters form a valid [First, Last) range iterator pair. This replaces _DEBUG_RANGE and supporting machinery. The standard library provides a version for pointers under _IDL != 0; otherwise they are normally provided via hidden friend functions. Note that declaring some of these hidden friends for "wrapper" iterators like move_iterator and reverse_iterator triggers VSO#610735. cit._Verify_offset(difference_type _Off) For random-access iterators, performs any asserts that would be performed by i += _Off; except with possibly a better error message and without moving the iterator. cit._Unwrapped() Returns an "unchecked" or "unwrapped" iterator which has previously been validated. The iterator may have been validated by a call to _Verify_range or _Verify_offset (above), or by seeking a checked iterator to a "high water mark" point. it._Seek_to(cit) / it._Seek_to(return value from _Unwrapped()) Moves the position of the checked iterator it to the position of the unchecked iterator supplied. Generally not intended to perform range checks. * Fixed build break in VS2015 * fixed constexpr build break when using VS2015
2018-05-22 21:07:49 -04:00
// algorithm call
Expects((index_ + n) >= 0 && (index_ + n) <= span_->size());
}
2020-02-03 16:52:00 -05:00
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
Add usage for check-and-unwrap of MS STL iterators (#687) * Add usage for check-and-unwrap of MS STL iterators This is Billy ONeal's PR #682 with a typo fixed. See corresponding change here: https://devdiv.visualstudio.com/DefaultCollection/DevDiv/_git/msvc/commit/ca77129308c775f422ecef0c01ad460f75c462ad That change officially exposes the STL's range checking machinery, available in MSVC++ "15.8"+ This change augments GSL span::iterator to call into that newly exposed machinery. _Verify_range(cit, cit) Requests that the iterator type check that the parameters form a valid [First, Last) range iterator pair. This replaces _DEBUG_RANGE and supporting machinery. The standard library provides a version for pointers under _IDL != 0; otherwise they are normally provided via hidden friend functions. Note that declaring some of these hidden friends for "wrapper" iterators like move_iterator and reverse_iterator triggers VSO#610735. cit._Verify_offset(difference_type _Off) For random-access iterators, performs any asserts that would be performed by i += _Off; except with possibly a better error message and without moving the iterator. cit._Unwrapped() Returns an "unchecked" or "unwrapped" iterator which has previously been validated. The iterator may have been validated by a call to _Verify_range or _Verify_offset (above), or by seeking a checked iterator to a "high water mark" point. it._Seek_to(cit) / it._Seek_to(return value from _Unwrapped()) Moves the position of the checked iterator it to the position of the unchecked iterator supplied. Generally not intended to perform range checks. * Fixed build break in VS2015 * fixed constexpr build break when using VS2015
2018-05-22 21:07:49 -04:00
constexpr pointer _Unwrapped() const noexcept
{ // after seeking *this to a high water mark, or using one of the
Add usage for check-and-unwrap of MS STL iterators (#687) * Add usage for check-and-unwrap of MS STL iterators This is Billy ONeal's PR #682 with a typo fixed. See corresponding change here: https://devdiv.visualstudio.com/DefaultCollection/DevDiv/_git/msvc/commit/ca77129308c775f422ecef0c01ad460f75c462ad That change officially exposes the STL's range checking machinery, available in MSVC++ "15.8"+ This change augments GSL span::iterator to call into that newly exposed machinery. _Verify_range(cit, cit) Requests that the iterator type check that the parameters form a valid [First, Last) range iterator pair. This replaces _DEBUG_RANGE and supporting machinery. The standard library provides a version for pointers under _IDL != 0; otherwise they are normally provided via hidden friend functions. Note that declaring some of these hidden friends for "wrapper" iterators like move_iterator and reverse_iterator triggers VSO#610735. cit._Verify_offset(difference_type _Off) For random-access iterators, performs any asserts that would be performed by i += _Off; except with possibly a better error message and without moving the iterator. cit._Unwrapped() Returns an "unchecked" or "unwrapped" iterator which has previously been validated. The iterator may have been validated by a call to _Verify_range or _Verify_offset (above), or by seeking a checked iterator to a "high water mark" point. it._Seek_to(cit) / it._Seek_to(return value from _Unwrapped()) Moves the position of the checked iterator it to the position of the unchecked iterator supplied. Generally not intended to perform range checks. * Fixed build break in VS2015 * fixed constexpr build break when using VS2015
2018-05-22 21:07:49 -04:00
// _Verify_xxx functions above, unwrap this span_iterator to a raw
// pointer
return span_->data() + index_;
}
// Tell the STL that span_iterator should not be unwrapped if it can't
// validate in advance, even in release / optimized builds:
#if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
static constexpr const bool _Unwrap_when_unverified = false;
#else
static constexpr bool _Unwrap_when_unverified = false;
#endif
2020-02-03 16:52:00 -05:00
GSL_SUPPRESS(con.3) // NO-FORMAT: attribute // TODO: false positive
Add usage for check-and-unwrap of MS STL iterators (#687) * Add usage for check-and-unwrap of MS STL iterators This is Billy ONeal's PR #682 with a typo fixed. See corresponding change here: https://devdiv.visualstudio.com/DefaultCollection/DevDiv/_git/msvc/commit/ca77129308c775f422ecef0c01ad460f75c462ad That change officially exposes the STL's range checking machinery, available in MSVC++ "15.8"+ This change augments GSL span::iterator to call into that newly exposed machinery. _Verify_range(cit, cit) Requests that the iterator type check that the parameters form a valid [First, Last) range iterator pair. This replaces _DEBUG_RANGE and supporting machinery. The standard library provides a version for pointers under _IDL != 0; otherwise they are normally provided via hidden friend functions. Note that declaring some of these hidden friends for "wrapper" iterators like move_iterator and reverse_iterator triggers VSO#610735. cit._Verify_offset(difference_type _Off) For random-access iterators, performs any asserts that would be performed by i += _Off; except with possibly a better error message and without moving the iterator. cit._Unwrapped() Returns an "unchecked" or "unwrapped" iterator which has previously been validated. The iterator may have been validated by a call to _Verify_range or _Verify_offset (above), or by seeking a checked iterator to a "high water mark" point. it._Seek_to(cit) / it._Seek_to(return value from _Unwrapped()) Moves the position of the checked iterator it to the position of the unchecked iterator supplied. Generally not intended to perform range checks. * Fixed build break in VS2015 * fixed constexpr build break when using VS2015
2018-05-22 21:07:49 -04:00
constexpr void _Seek_to(const pointer p) noexcept
{ // adjust the position of *this to previously verified location p
Add usage for check-and-unwrap of MS STL iterators (#687) * Add usage for check-and-unwrap of MS STL iterators This is Billy ONeal's PR #682 with a typo fixed. See corresponding change here: https://devdiv.visualstudio.com/DefaultCollection/DevDiv/_git/msvc/commit/ca77129308c775f422ecef0c01ad460f75c462ad That change officially exposes the STL's range checking machinery, available in MSVC++ "15.8"+ This change augments GSL span::iterator to call into that newly exposed machinery. _Verify_range(cit, cit) Requests that the iterator type check that the parameters form a valid [First, Last) range iterator pair. This replaces _DEBUG_RANGE and supporting machinery. The standard library provides a version for pointers under _IDL != 0; otherwise they are normally provided via hidden friend functions. Note that declaring some of these hidden friends for "wrapper" iterators like move_iterator and reverse_iterator triggers VSO#610735. cit._Verify_offset(difference_type _Off) For random-access iterators, performs any asserts that would be performed by i += _Off; except with possibly a better error message and without moving the iterator. cit._Unwrapped() Returns an "unchecked" or "unwrapped" iterator which has previously been validated. The iterator may have been validated by a call to _Verify_range or _Verify_offset (above), or by seeking a checked iterator to a "high water mark" point. it._Seek_to(cit) / it._Seek_to(return value from _Unwrapped()) Moves the position of the checked iterator it to the position of the unchecked iterator supplied. Generally not intended to perform range checks. * Fixed build break in VS2015 * fixed constexpr build break when using VS2015
2018-05-22 21:07:49 -04:00
// after _Unwrapped
index_ = p - span_->data();
}
#endif
protected:
const Span* span_ = nullptr;
std::ptrdiff_t index_ = 0;
2016-07-20 16:17:47 -04:00
};
template <std::size_t Ext>
class extent_type
{
public:
using index_type = std::size_t;
static_assert(Ext >= 0, "A fixed-size span must be >= 0 in size.");
constexpr extent_type() noexcept {}
template <index_type Other>
constexpr extent_type(extent_type<Other> ext)
{
static_assert(Other == Ext || Other == dynamic_extent,
"Mismatch between fixed-size extent and size of initializing data.");
Expects(ext.size() == Ext);
}
constexpr extent_type(index_type size) { Expects(size == Ext); }
constexpr index_type size() const noexcept { return Ext; }
};
template <>
class extent_type<dynamic_extent>
{
public:
using index_type = std::size_t;
template <index_type Other>
explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size())
{}
explicit constexpr extent_type(index_type size) : size_(size)
{
Expects(size != dynamic_extent);
}
constexpr index_type size() const noexcept { return size_; }
private:
index_type size_;
};
template <class ElementType, std::size_t Extent, std::size_t Offset, std::size_t Count>
struct calculate_subspan_type
{
using type = span<ElementType, Count != dynamic_extent
? Count
: (Extent != dynamic_extent ? Extent - Offset : Extent)>;
};
2016-03-01 15:11:41 -05:00
} // namespace details
2016-07-20 16:17:47 -04:00
// [span], class template span
template <class ElementType, std::size_t Extent>
2016-07-20 16:17:47 -04:00
class span
{
public:
2016-07-20 16:17:47 -04:00
// constants and types
using element_type = ElementType;
using value_type = std::remove_cv_t<ElementType>;
using index_type = std::size_t;
using pointer = element_type*;
using reference = element_type&;
using difference_type = std::ptrdiff_t;
2016-05-29 16:52:28 -04:00
using iterator = details::span_iterator<span<ElementType, Extent>, false>;
using const_iterator = details::span_iterator<span<ElementType, Extent>, true>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
2016-05-29 16:52:28 -04:00
using size_type = index_type;
#if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
static constexpr const index_type extent{Extent};
#else
static constexpr index_type extent{Extent};
#endif
2016-07-20 16:17:47 -04:00
// [span.cons], span constructors, copy, assignment, and destructor
template <bool Dependent = false,
// "Dependent" is needed to make "std::enable_if_t<Dependent || Extent <= 0>" SFINAE,
// since "std::enable_if_t<Extent <= 0>" is ill-formed when Extent is greater than 0.
class = std::enable_if_t<(Dependent || Extent == 0 || Extent == dynamic_extent)>>
constexpr span() noexcept : storage_(nullptr, details::extent_type<0>())
{}
2016-02-25 14:42:26 -05:00
constexpr span(pointer ptr, index_type count) noexcept : storage_(ptr, count) {}
2016-02-25 14:42:26 -05:00
constexpr span(pointer firstElem, pointer lastElem) noexcept
: storage_(firstElem, std::distance(firstElem, lastElem))
{}
2016-07-20 16:17:47 -04:00
2017-02-13 15:11:45 -05:00
template <std::size_t N>
constexpr span(element_type (&arr)[N]) noexcept
: storage_(KnownNotNull{std::addressof(arr[0])}, details::extent_type<N>())
{}
2016-07-26 22:19:47 -04:00
template <std::size_t N, class = std::enable_if_t<(N > 0)>>
constexpr span(std::array<std::remove_const_t<element_type>, N>& arr) noexcept
: storage_(KnownNotNull{arr.data()}, details::extent_type<N>())
{}
constexpr span(std::array<std::remove_const_t<element_type>, 0>&) noexcept
: storage_(static_cast<pointer>(nullptr), details::extent_type<0>())
{}
template <std::size_t N, class = std::enable_if_t<(N > 0)>>
constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr) noexcept
: storage_(KnownNotNull{arr.data()}, details::extent_type<N>())
{}
constexpr span(const std::array<std::remove_const_t<element_type>, 0>&) noexcept
: storage_(static_cast<pointer>(nullptr), details::extent_type<0>())
{}
2016-02-29 16:16:48 -05:00
2016-03-01 15:11:41 -05:00
// NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement
// on Container to be a contiguous sequence container.
template <class Container,
2016-07-20 16:17:47 -04:00
class = std::enable_if_t<
2016-07-26 22:19:47 -04:00
!details::is_span<Container>::value && !details::is_std_array<Container>::value &&
std::is_convertible<typename Container::pointer, pointer>::value &&
std::is_convertible<typename Container::pointer,
2016-07-20 16:17:47 -04:00
decltype(std::declval<Container>().data())>::value>>
constexpr span(Container& cont) noexcept : span(cont.data(), narrow<index_type>(cont.size()))
{}
2016-03-01 15:11:41 -05:00
template <class Container,
2016-07-20 16:17:47 -04:00
class = std::enable_if_t<
std::is_const<element_type>::value && !details::is_span<Container>::value &&
std::is_convertible<typename Container::pointer, pointer>::value &&
std::is_convertible<typename Container::pointer,
2016-07-20 16:17:47 -04:00
decltype(std::declval<Container>().data())>::value>>
constexpr span(const Container& cont) noexcept
: span(cont.data(), narrow<index_type>(cont.size()))
{}
constexpr span(const span& other) noexcept = default;
2016-03-16 22:39:55 -04:00
2016-07-20 16:17:47 -04:00
template <
class OtherElementType, std::size_t OtherExtent,
class = std::enable_if_t<
2016-07-20 16:17:47 -04:00
details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
details::is_allowed_element_type_conversion<OtherElementType, element_type>::value>>
constexpr span(const span<OtherElementType, OtherExtent>& other) noexcept
: storage_(other.data(), details::extent_type<OtherExtent>(other.size()))
{}
2016-03-16 22:39:55 -04:00
~span() noexcept = default;
constexpr span& operator=(const span& other) noexcept = default;
2016-07-20 16:17:47 -04:00
// [span.sub], span subviews
template <std::size_t Count>
constexpr span<element_type, Count> first() const noexcept
2016-03-18 19:49:29 -04:00
{
Expects(Count <= size());
2016-07-20 16:17:47 -04:00
return {data(), Count};
2016-03-18 19:49:29 -04:00
}
template <std::size_t Count>
2020-02-03 16:52:00 -05:00
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
constexpr span<element_type, Count> last() const noexcept
2016-03-18 19:49:29 -04:00
{
Expects(size() >= Count);
2016-07-20 16:17:47 -04:00
return {data() + (size() - Count), Count};
2016-03-18 19:49:29 -04:00
}
template <std::size_t Offset, std::size_t Count = dynamic_extent>
2020-02-03 16:52:00 -05:00
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
constexpr auto subspan() const noexcept ->
typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type
2016-03-18 19:49:29 -04:00
{
Expects((size() >= Offset) &&
2020-02-03 15:46:37 -05:00
(Count == dynamic_extent || (Offset + Count <= size())));
2016-07-20 16:17:47 -04:00
return {data() + Offset, Count == dynamic_extent ? size() - Offset : Count};
2016-03-18 19:49:29 -04:00
}
constexpr span<element_type, dynamic_extent> first(index_type count) const noexcept
2016-03-18 19:49:29 -04:00
{
Expects(count <= size());
2016-07-20 16:17:47 -04:00
return {data(), count};
2016-03-18 19:49:29 -04:00
}
constexpr span<element_type, dynamic_extent> last(index_type count) const noexcept
2016-03-18 19:49:29 -04:00
{
Expects(count <= size());
return make_subspan(size() - count, dynamic_extent, subspan_selector<Extent>{});
2016-03-18 19:49:29 -04:00
}
constexpr span<element_type, dynamic_extent> subspan(index_type offset,
index_type count = dynamic_extent) const
noexcept
2016-03-18 19:49:29 -04:00
{
return make_subspan(offset, count, subspan_selector<Extent>{});
2016-03-18 19:49:29 -04:00
}
2016-07-20 16:17:47 -04:00
// [span.obs], span observers
constexpr index_type size() const noexcept { return storage_.size(); }
constexpr index_type size_bytes() const noexcept
{
return size() * narrow_cast<index_type>(sizeof(element_type));
}
constexpr bool empty() const noexcept { return size() == 0; }
2016-07-20 16:17:47 -04:00
// [span.elem], span element access
2020-02-03 16:52:00 -05:00
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
constexpr reference operator[](index_type idx) const noexcept
2016-02-25 14:42:26 -05:00
{
Expects(CheckRange(idx, storage_.size()));
2016-07-20 12:24:49 -04:00
return data()[idx];
2016-02-25 14:42:26 -05:00
}
2020-01-09 15:13:12 -05:00
constexpr reference front() const noexcept
2019-11-19 19:03:55 -05:00
{
Expects(size() != dynamic_extent && size() > 0);
2019-11-19 19:03:55 -05:00
return data()[0];
}
2020-01-09 15:13:12 -05:00
constexpr reference back() const noexcept
2019-11-19 19:03:55 -05:00
{
Expects(size() != dynamic_extent && size() > 0);
2019-11-19 19:03:55 -05:00
return data()[size() - 1];
}
// at and operator() are deprecated to align to the public member functions of std::span
[[deprecated("Use operator[]")]] constexpr reference at(index_type idx) const noexcept
{
return this->operator[](idx);
}
[[deprecated("Use operator[]")]] constexpr reference operator()(index_type idx) const noexcept
{
return this->operator[](idx);
}
constexpr pointer data() const noexcept { return storage_.data(); }
2016-05-29 16:52:28 -04:00
2016-07-20 16:17:47 -04:00
// [span.iter], span iterator support
constexpr iterator begin() const noexcept { return {this, 0}; }
constexpr iterator end() const noexcept { return {this, size()}; }
constexpr const_iterator cbegin() const noexcept { return {this, 0}; }
constexpr const_iterator cend() const noexcept { return {this, size()}; }
2016-07-20 16:17:47 -04:00
constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; }
constexpr reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; }
constexpr const_reverse_iterator crbegin() const noexcept
{
return const_reverse_iterator{cend()};
}
constexpr const_reverse_iterator crend() const noexcept
{
return const_reverse_iterator{cbegin()};
}
#ifdef _MSC_VER
// Tell MSVC how to unwrap spans in range-based-for
constexpr pointer _Unchecked_begin() const noexcept { return data(); }
constexpr pointer _Unchecked_end() const noexcept
{
2020-02-03 16:52:00 -05:00
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
return data() + size();
}
#endif // _MSC_VER
private:
static constexpr bool CheckRange(index_type idx, index_type size) noexcept
{
// Optimization:
//
// idx >= 0 && idx < size
// =>
// static_cast<size_t>(idx) < static_cast<size_t>(size)
//
// because size >=0 by span construction, and negative idx will
// wrap around to a value always greater than size when casted.
// check if we have enough space to wrap around
#if defined(__cpp_if_constexpr)
if constexpr (sizeof(index_type) <= sizeof(size_t))
#else
if (sizeof(index_type) <= sizeof(size_t))
#endif
{
return narrow_cast<size_t>(idx) < narrow_cast<size_t>(size);
}
else
{
2020-02-03 15:26:15 -05:00
return idx < size;
}
}
// Needed to remove unnecessary null check in subspans
struct KnownNotNull
{
pointer p;
};
2016-07-20 16:17:47 -04:00
// this implementation detail class lets us take advantage of the
// empty base class optimization to pay for only storage of a single
// pointer in the case of fixed-size spans
template <class ExtentType>
class storage_type : public ExtentType
{
public:
// KnownNotNull parameter is needed to remove unnecessary null check
// in subspans and constructors from arrays
template <class OtherExtentType>
constexpr storage_type(KnownNotNull data, OtherExtentType ext)
: ExtentType(ext), data_(data.p)
{
Expects(ExtentType::size() != dynamic_extent);
}
template <class OtherExtentType>
constexpr storage_type(pointer data, OtherExtentType ext) : ExtentType(ext), data_(data)
2016-07-20 16:17:47 -04:00
{
Expects(ExtentType::size() != dynamic_extent);
Expects(data || ExtentType::size() == 0);
2016-07-20 16:17:47 -04:00
}
constexpr pointer data() const noexcept { return data_; }
private:
pointer data_;
};
storage_type<details::extent_type<Extent>> storage_;
// The rest is needed to remove unnecessary null check
// in subspans and constructors from arrays
constexpr span(KnownNotNull ptr, index_type count) : storage_(ptr, count) {}
template <std::size_t CallerExtent>
class subspan_selector
{
};
template <std::size_t CallerExtent>
span<element_type, dynamic_extent> make_subspan(index_type offset, index_type count,
subspan_selector<CallerExtent>) const
{
const span<element_type, dynamic_extent> tmp(*this);
return tmp.subspan(offset, count);
}
2020-02-03 16:52:00 -05:00
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
span<element_type, dynamic_extent> make_subspan(index_type offset, index_type count,
subspan_selector<dynamic_extent>) const
{
2020-02-03 15:46:37 -05:00
Expects(size() >= offset && size() != dynamic_extent);
if (count == dynamic_extent) { return {KnownNotNull{data() + offset}, size() - offset}; }
Expects(size() - offset >= count);
return {KnownNotNull{data() + offset}, count};
}
};
#if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
template <class ElementType, std::size_t Extent>
constexpr const typename span<ElementType, Extent>::index_type span<ElementType, Extent>::extent;
#endif
2016-07-20 16:17:47 -04:00
// [span.comparison], span comparison operators
template <class ElementType, std::size_t FirstExtent, std::size_t SecondExtent>
constexpr bool operator==(span<ElementType, FirstExtent> l, span<ElementType, SecondExtent> r)
2016-07-20 16:17:47 -04:00
{
return std::equal(l.begin(), l.end(), r.begin(), r.end());
}
template <class ElementType, std::size_t Extent>
constexpr bool operator!=(span<ElementType, Extent> l, span<ElementType, Extent> r)
2016-07-20 16:17:47 -04:00
{
return !(l == r);
}
template <class ElementType, std::size_t Extent>
constexpr bool operator<(span<ElementType, Extent> l, span<ElementType, Extent> r)
2016-07-20 16:17:47 -04:00
{
return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
}
template <class ElementType, std::size_t Extent>
constexpr bool operator<=(span<ElementType, Extent> l, span<ElementType, Extent> r)
2016-07-20 16:17:47 -04:00
{
return !(l > r);
}
template <class ElementType, std::size_t Extent>
constexpr bool operator>(span<ElementType, Extent> l, span<ElementType, Extent> r)
2016-07-20 16:17:47 -04:00
{
return r < l;
}
template <class ElementType, std::size_t Extent>
constexpr bool operator>=(span<ElementType, Extent> l, span<ElementType, Extent> r)
2016-07-20 16:17:47 -04:00
{
return !(l < r);
}
2016-05-29 20:06:29 -04:00
namespace details
{
// if we only supported compilers with good constexpr support then
// this pair of classes could collapse down to a constexpr function
2017-02-13 15:11:45 -05:00
// we should use a narrow_cast<> to go to std::size_t, but older compilers may not see it as
2016-07-20 16:17:47 -04:00
// constexpr
2016-05-29 20:06:29 -04:00
// and so will fail compilation of the template
template <class ElementType, std::size_t Extent>
struct calculate_byte_size : std::integral_constant<std::size_t, sizeof(ElementType) * Extent>
2016-07-20 16:17:47 -04:00
{
};
2016-05-29 20:06:29 -04:00
template <class ElementType>
2016-07-20 16:17:47 -04:00
struct calculate_byte_size<ElementType, dynamic_extent>
: std::integral_constant<std::size_t, dynamic_extent>
2016-07-20 16:17:47 -04:00
{
};
} // namespace details
2016-05-29 20:06:29 -04:00
2016-07-20 16:17:47 -04:00
// [span.objectrep], views of object representation
template <class ElementType, std::size_t Extent>
2016-07-20 16:17:47 -04:00
span<const byte, details::calculate_byte_size<ElementType, Extent>::value>
as_bytes(span<ElementType, Extent> s) noexcept
2016-07-20 16:17:47 -04:00
{
2020-02-03 16:52:00 -05:00
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
2016-07-20 16:17:47 -04:00
return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
}
template <class ElementType, std::size_t Extent,
2016-07-20 16:17:47 -04:00
class = std::enable_if_t<!std::is_const<ElementType>::value>>
span<byte, details::calculate_byte_size<ElementType, Extent>::value>
as_writeable_bytes(span<ElementType, Extent> s) noexcept
2016-07-20 16:17:47 -04:00
{
2020-02-03 16:52:00 -05:00
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
2016-07-20 16:17:47 -04:00
return {reinterpret_cast<byte*>(s.data()), s.size_bytes()};
}
2016-02-24 14:26:28 -05:00
//
// make_span() - Utility functions for creating spans
//
template <class ElementType>
constexpr span<ElementType> make_span(ElementType* ptr,
typename span<ElementType>::index_type count)
{
return span<ElementType>(ptr, count);
}
template <class ElementType>
2018-02-23 16:35:36 -05:00
constexpr span<ElementType> make_span(ElementType* firstElem, ElementType* lastElem)
{
return span<ElementType>(firstElem, lastElem);
}
2017-02-13 15:11:45 -05:00
template <class ElementType, std::size_t N>
constexpr span<ElementType, N> make_span(ElementType (&arr)[N]) noexcept
{
return span<ElementType, N>(arr);
}
template <class Container>
2018-02-23 16:35:36 -05:00
constexpr span<typename Container::value_type> make_span(Container& cont)
{
return span<typename Container::value_type>(cont);
}
template <class Container>
2018-02-23 16:35:36 -05:00
constexpr span<const typename Container::value_type> make_span(const Container& cont)
{
return span<const typename Container::value_type>(cont);
}
template <class Ptr>
constexpr span<typename Ptr::element_type> make_span(Ptr& cont, std::size_t count)
{
return span<typename Ptr::element_type>(cont, count);
}
template <class Ptr>
2018-02-23 16:35:36 -05:00
constexpr span<typename Ptr::element_type> make_span(Ptr& cont)
{
return span<typename Ptr::element_type>(cont);
}
// Specialization of gsl::at for span
template <class ElementType, std::size_t Extent>
constexpr ElementType& at(span<ElementType, Extent> s, index i)
{
// No bounds checking here because it is done in span::operator[] called below
return s[i];
}
// [span.obs] Free observer functions
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::index_type
ssize(const span<ElementType, Extent>& span) noexcept
{
return span.size();
}
// [span.iter] Free functions for begin/end functions
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::iterator
begin(const span<ElementType, Extent>& span) noexcept
{
return span.begin();
}
template <class ElementType, std::size_t Extent = dynamic_extent>
constexpr typename span<ElementType, Extent>::iterator
end(const span<ElementType, Extent>& span) noexcept
{
return span.end();
}
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::const_iterator
cbegin(const span<ElementType, Extent>& span) noexcept
{
return span.cbegin();
}
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::const_iterator
cend(const span<ElementType, Extent>& span) noexcept
{
return span.cend();
}
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::reverse_iterator
rbegin(const span<ElementType, Extent>& span) noexcept
{
return span.rbegin();
}
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::reverse_iterator
rend(const span<ElementType, Extent>& span) noexcept
{
return span.rend();
}
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::const_reverse_iterator
crbegin(const span<ElementType, Extent>& span) noexcept
{
return span.crbegin();
}
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::const_reverse_iterator
crend(const span<ElementType, Extent>& span) noexcept
{
return span.crend();
}
2016-02-24 14:26:28 -05:00
} // namespace gsl
Clang cl (#762) * Added c++17 test configurations for clang5.0 and clang6.0 * Fix #739 correct cppcorecheck warnings for clang-cl * Add clang-cl configurations * Corrections Appveyor; Temporarily disable msvc compilation for faster testing (#741) * Add path to clang-cl.exe (#741) * Escape backslash in path (#741) * Update vcpkg (#741) * Check vcpkg version; try without building vcpkg; use latest clang-cl from path (#741) * Fix blocks in ps script (#741) * Try accessing APPVEYOR_BUILD_FOLDER variable (#471) * Update span size() bug confirmation test for GCC 6.5 (#741) * MSVC flags to Clang-cl; disable c++98-compat and undefined macro warnings (#741) * Suppress clang warning on tests (missing-prototypes) (#741) * Fix clang warning -Wnewline-eof (#741) * Fix clang warning -Wdouble-promotion (#741) * Set linker explicitly * Clean condition statement * For Clang, fallback to the AppVeyor installed version of CMake * Fix clang warning -Wmissing-variable-declarations * Fallback to the MSVC linker until vcpkg has been updated * Revert "Fallback to the MSVC linker until vcpkg has been updated" This reverts commit 7263f3289e6e835c32fc7800d0c2d4e58bd21178. * Fix clang warning -Wunused-member-function * Fix clang warning -Wmissing-noreturn * Fix clang warning -Winvalid-noreturn on Windows * Add macro block end comment on large #if blocks * Workaround: fallback to mscv link.exe * Workaround: get msvc paths into PowerShell through intermediate file * Workaround: fix, remove "PATH=" from text * Workaround: try with full-path; and return user PATH * Workaround: fix, escape backslashes * Revert all since "Workaround: fallback to mscv link.exe" did not work on AppVeyor This reverts the commits: bda3d6a428e5d19e97375b2e575b0f51ff1b4bc0 97062933acfb6428535c0fdcab5d94371ce72bfc 0f4fb04bac9c2d091005b791294237250dbe0668 1b0c19afd154f4ffc5ef793014c1bc2324534fd0 a5739ea5f0cd7633527b5e5d3b16a9e3ade7149f * Suppress output of git pull; remove vcpkg from cache * Re-enable AppVeyor builds for all platforms * Correct typo Co-Authored-By: Farwaykorse <Farwaykorse@users.noreply.github.com> * Add Clang-cl 7.0.0 to the supported platforms * Revert "Fix clang warning -Wunused-member-function" This reverts commit 6fe1a4203501145ab4350b9152ecc11f3a30e49a. * Fix or locally suppress clang warning -Wunused-member-function * format touched code and correct comment * git pull --quiet * fix logic error in workaround * fix missing bracket * Suppress output of mkdir * Replace MSBuild with Ninja * Suppress output of 7z * Add architecture flags for Clang * Drop workaround for lld-link * 7-zip Overwrite and Alternative output suppression without suppressing errors Replaces 3c1c0794dd9a29908cc7c38f591bd3c7b4929677 * AppVeyor setup and CMake before build * reorder compiler configuration * remove unnecessary * remove -fno-strict-aliasing * remove -Wsign-conversion, since before v4.0 part of -Wconversion * -Wctor-dtor-privacy is GCC only * remove -Woverloaded-virtual part of -Wmost, part of -Wall * add -Wmissing-noreturn * remove the pragmas for -Wunused-member-function * Re-add MSBuild generator on AppVeyor * Print CMake commands * Add MSBuild toolset selection * Separate Architecture setting * clang-cl: add -Weverything * clang-cl -Wno-c++98-compat * clang-cl -Wno-c++98-compat-pedantic * clang-cl -Wno-missing-prototypes * clang-cl C++14 -Wno-unused-member-function * clang-cl -Wundef __GNUC__ * clang++: add -Weverything * clang++ -Wno-c++98-compat * clang++ -Wno-c++98-compat-pedantic * clang++ -Wno-missing-prototypes * clang++ -Wno-weak-vtables * clang++ C++14 -Wno-unused-member-function * clang++ fix -Wundef _MSC_VER * clang++ -Wno-padded * clang++ solve -Wdeprecated * Add AppleClang compiler target Since CMake v3.0 use of Clang for both is deprecated * clang++ v5.0 C++17 -Wno-undefined-func-template * Add VS2015 + LLVM/clang-cl to AppVeyor * Do not disable constexpr when compiling with clang-cl on Windows * Clean-up clang-only warnings (now under -Weverything) * Revert "Fix clang warning -Winvalid-noreturn on Windows" This reverts commit 2238c4760e86feebb2a18620b77312bd01055f61. * Suppress -Winvalid-noreturn for the MS STL noexception workaround * CMake: put preprocessor definition in target_compile_definitions * Solve compiler warning C4668: __GNUC__ not defined
2019-01-15 13:27:34 -05:00
#if defined(_MSC_VER) && !defined(__clang__)
#if _MSC_VER < 1910
#undef constexpr
#pragma pop_macro("constexpr")
2016-02-24 14:26:28 -05:00
#endif // _MSC_VER < 1910
2016-02-24 14:26:28 -05:00
#pragma warning(pop)
2016-02-24 14:26:28 -05:00
#endif // _MSC_VER
Clang cl (#762) * Added c++17 test configurations for clang5.0 and clang6.0 * Fix #739 correct cppcorecheck warnings for clang-cl * Add clang-cl configurations * Corrections Appveyor; Temporarily disable msvc compilation for faster testing (#741) * Add path to clang-cl.exe (#741) * Escape backslash in path (#741) * Update vcpkg (#741) * Check vcpkg version; try without building vcpkg; use latest clang-cl from path (#741) * Fix blocks in ps script (#741) * Try accessing APPVEYOR_BUILD_FOLDER variable (#471) * Update span size() bug confirmation test for GCC 6.5 (#741) * MSVC flags to Clang-cl; disable c++98-compat and undefined macro warnings (#741) * Suppress clang warning on tests (missing-prototypes) (#741) * Fix clang warning -Wnewline-eof (#741) * Fix clang warning -Wdouble-promotion (#741) * Set linker explicitly * Clean condition statement * For Clang, fallback to the AppVeyor installed version of CMake * Fix clang warning -Wmissing-variable-declarations * Fallback to the MSVC linker until vcpkg has been updated * Revert "Fallback to the MSVC linker until vcpkg has been updated" This reverts commit 7263f3289e6e835c32fc7800d0c2d4e58bd21178. * Fix clang warning -Wunused-member-function * Fix clang warning -Wmissing-noreturn * Fix clang warning -Winvalid-noreturn on Windows * Add macro block end comment on large #if blocks * Workaround: fallback to mscv link.exe * Workaround: get msvc paths into PowerShell through intermediate file * Workaround: fix, remove "PATH=" from text * Workaround: try with full-path; and return user PATH * Workaround: fix, escape backslashes * Revert all since "Workaround: fallback to mscv link.exe" did not work on AppVeyor This reverts the commits: bda3d6a428e5d19e97375b2e575b0f51ff1b4bc0 97062933acfb6428535c0fdcab5d94371ce72bfc 0f4fb04bac9c2d091005b791294237250dbe0668 1b0c19afd154f4ffc5ef793014c1bc2324534fd0 a5739ea5f0cd7633527b5e5d3b16a9e3ade7149f * Suppress output of git pull; remove vcpkg from cache * Re-enable AppVeyor builds for all platforms * Correct typo Co-Authored-By: Farwaykorse <Farwaykorse@users.noreply.github.com> * Add Clang-cl 7.0.0 to the supported platforms * Revert "Fix clang warning -Wunused-member-function" This reverts commit 6fe1a4203501145ab4350b9152ecc11f3a30e49a. * Fix or locally suppress clang warning -Wunused-member-function * format touched code and correct comment * git pull --quiet * fix logic error in workaround * fix missing bracket * Suppress output of mkdir * Replace MSBuild with Ninja * Suppress output of 7z * Add architecture flags for Clang * Drop workaround for lld-link * 7-zip Overwrite and Alternative output suppression without suppressing errors Replaces 3c1c0794dd9a29908cc7c38f591bd3c7b4929677 * AppVeyor setup and CMake before build * reorder compiler configuration * remove unnecessary * remove -fno-strict-aliasing * remove -Wsign-conversion, since before v4.0 part of -Wconversion * -Wctor-dtor-privacy is GCC only * remove -Woverloaded-virtual part of -Wmost, part of -Wall * add -Wmissing-noreturn * remove the pragmas for -Wunused-member-function * Re-add MSBuild generator on AppVeyor * Print CMake commands * Add MSBuild toolset selection * Separate Architecture setting * clang-cl: add -Weverything * clang-cl -Wno-c++98-compat * clang-cl -Wno-c++98-compat-pedantic * clang-cl -Wno-missing-prototypes * clang-cl C++14 -Wno-unused-member-function * clang-cl -Wundef __GNUC__ * clang++: add -Weverything * clang++ -Wno-c++98-compat * clang++ -Wno-c++98-compat-pedantic * clang++ -Wno-missing-prototypes * clang++ -Wno-weak-vtables * clang++ C++14 -Wno-unused-member-function * clang++ fix -Wundef _MSC_VER * clang++ -Wno-padded * clang++ solve -Wdeprecated * Add AppleClang compiler target Since CMake v3.0 use of Clang for both is deprecated * clang++ v5.0 C++17 -Wno-undefined-func-template * Add VS2015 + LLVM/clang-cl to AppVeyor * Do not disable constexpr when compiling with clang-cl on Windows * Clean-up clang-only warnings (now under -Weverything) * Revert "Fix clang warning -Winvalid-noreturn on Windows" This reverts commit 2238c4760e86feebb2a18620b77312bd01055f61. * Suppress -Winvalid-noreturn for the MS STL noexception workaround * CMake: put preprocessor definition in target_compile_definitions * Solve compiler warning C4668: __GNUC__ not defined
2019-01-15 13:27:34 -05:00
#if defined(__GNUC__) && __GNUC__ > 6
#pragma GCC diagnostic pop
#endif // __GNUC__ > 6
2016-02-24 14:26:28 -05:00
#endif // GSL_SPAN_H