diff options
author | Dmitry Kobets <dmitrykobets@microsoft.com> | 2022-09-27 01:27:01 +0300 |
---|---|---|
committer | Dmitry Kobets <dmitrykobets@microsoft.com> | 2022-09-27 01:27:01 +0300 |
commit | 8840d871995e06e36ac7b2c1f3a1d1a9c447365f (patch) | |
tree | 553888391284f68e7791cc35764ff6d1a16c2c20 /include/gsl/util | |
parent | d569ed65d05791fa6589115e1bfea879c8e9c591 (diff) | |
parent | 10df83d292bf5bbdc487e57dc8c2dc8c7a01f4d1 (diff) |
Merge remote-tracking branch 'origin/main' into final_action-revision
Diffstat (limited to 'include/gsl/util')
-rw-r--r-- | include/gsl/util | 35 |
1 files changed, 28 insertions, 7 deletions
diff --git a/include/gsl/util b/include/gsl/util index 8d3f381..db6a85e 100644 --- a/include/gsl/util +++ b/include/gsl/util @@ -17,7 +17,7 @@ #ifndef GSL_UTIL_H #define GSL_UTIL_H -#include <gsl/assert> // for Expects +#include "assert" // for Expects #include <array> #include <cstddef> // for ptrdiff_t, size_t @@ -25,6 +25,13 @@ #include <type_traits> // for is_signed, integral_constant #include <utility> // for exchange, forward +#if defined(__has_include) && __has_include(<version>) +#include <version> +#if defined(__cpp_lib_span) && __cpp_lib_span >= 202002L +#include <span> +#endif // __cpp_lib_span >= 202002L +#endif //__has_include(<version>) + #if defined(_MSC_VER) && !defined(__clang__) #pragma warning(push) @@ -38,6 +45,12 @@ #define GSL_NODISCARD #endif // defined(__cplusplus) && (__cplusplus >= 201703L) +#if defined(__cpp_inline_variables) +#define GSL_INLINE inline +#else +#define GSL_INLINE +#endif + namespace gsl { // @@ -81,8 +94,8 @@ GSL_NODISCARD auto finally(F&& f) noexcept template <class T, class U> // clang-format off GSL_SUPPRESS(type.1) // NO-FORMAT: attribute -// clang-format on -constexpr T narrow_cast(U&& u) noexcept + // clang-format on + constexpr T narrow_cast(U&& u) noexcept { return static_cast<T>(std::forward<U>(u)); } @@ -94,7 +107,7 @@ template <class T, std::size_t N> // clang-format off GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute -// clang-format on + // clang-format on constexpr T& at(T (&arr)[N], const index i) { Expects(i >= 0 && i < narrow_cast<index>(N)); @@ -105,7 +118,7 @@ template <class Cont> // clang-format off GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute -// clang-format on + // clang-format on constexpr auto at(Cont& cont, const index i) -> decltype(cont[cont.size()]) { Expects(i >= 0 && i < narrow_cast<index>(cont.size())); @@ -116,13 +129,21 @@ GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute template <class T> // clang-format off GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute -// clang-format on -constexpr T at(const std::initializer_list<T> cont, const index i) + // clang-format on + constexpr T at(const std::initializer_list<T> cont, const index i) { Expects(i >= 0 && i < narrow_cast<index>(cont.size())); return *(cont.begin() + i); } +#if defined(__cpp_lib_span) && __cpp_lib_span >= 202002L +template <class T, size_t extent = std::dynamic_extent> +constexpr auto at(std::span<T, extent> sp, const index i) -> decltype(sp[sp.size()]) +{ + Expects(i >= 0 && i < narrow_cast<index>(sp.size())); + return sp[gsl::narrow_cast<size_t>(i)]; +} +#endif // __cpp_lib_span >= 202002L } // namespace gsl #if defined(_MSC_VER) && !defined(__clang__) |