From d68265510617ca19ccfd60b0e9f92d86a309a265 Mon Sep 17 00:00:00 2001 From: Werner Henze Date: Thu, 27 Feb 2025 14:46:58 +0100 Subject: [PATCH] replace BYTE_TYPE with a solution based on namespaces - A macro with the very generic name `BYTE_TYPE` is likely to collide with existing code, so get rid of the macro. - The new solution is to provide a non-deprecated `byte` in the namespace `gsl::impl`. - Users of GSL should use `gsl::byte`, which is still deprecated when mapped to a `std::std::byte`. - GSL types and functions need to use `gsl::impl::byte` so they do not trigger the deprecation warning. - The `gsl::impl::byte` return type in an exported function is not nice, it might mislead users to use that type in their own declarations. But the `BYTE_TYPE` solution is not better in this respect. --- include/gsl/byte | 26 ++++++++++++++++---------- include/gsl/span | 14 +++++++------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/include/gsl/byte b/include/gsl/byte index 7e4c622..fe9b286 100644 --- a/include/gsl/byte +++ b/include/gsl/byte @@ -74,12 +74,6 @@ #define byte_may_alias #endif // defined __clang__ || defined __GNUC__ -#if GSL_USE_STD_BYTE -#define BYTE_TYPE std::byte -#else // !GSL_USE_STD_BYTE -#define BYTE_TYPE byte -#endif // GSL_USE_STD_BYTE - #if GSL_USE_STD_BYTE #include #endif @@ -88,6 +82,12 @@ namespace gsl { #if GSL_USE_STD_BYTE +namespace impl { +// impl::byte is used by gsl::as_bytes so our own code does not trigger a deprecation warning as would be the case when we used gsl::byte. +// Users of GSL should only use gsl::byte, not gsl::impl::byte. +using byte = std::byte; +} + using byte GSL_DEPRECATED("Use std::byte instead.") = std::byte; using std::to_integer; @@ -100,6 +100,12 @@ enum class byte_may_alias byte : unsigned char { }; +namespace impl { +// impl::byte is used by gsl::as_bytes so our own code does not trigger a deprecation warning as would be the case when we used gsl::byte. +// Users of GSL should only use gsl::byte, not gsl::impl::byte. +using byte = gsl::byte; +} + template ::value, bool> = true> constexpr byte& operator<<=(byte& b, IntegerType shift) noexcept { @@ -168,20 +174,20 @@ constexpr IntegerType to_integer(byte b) noexcept template // NOTE: need suppression since c++14 does not allow "return {t}" // GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: suppression does not work -constexpr BYTE_TYPE to_byte(T t) noexcept +constexpr gsl::impl::byte to_byte(T t) noexcept { static_assert(std::is_same::value, "gsl::to_byte(t) must be provided an unsigned char, otherwise data loss may occur. " "If you are calling to_byte with an integer constant use: gsl::to_byte() version."); - return BYTE_TYPE(t); + return gsl::impl::byte(t); } template -constexpr BYTE_TYPE to_byte() noexcept +constexpr gsl::impl::byte to_byte() noexcept { static_assert(I >= 0 && I <= 255, "gsl::byte only has 8 bits of storage, values must be in range 0-255"); - return static_cast(I); + return static_cast(I); } } // namespace gsl diff --git a/include/gsl/span b/include/gsl/span index 42b083c..cef8a77 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -18,7 +18,7 @@ #define GSL_SPAN_H #include "./assert" // for Expects -#include "./byte" // for BYTE_TYPE +#include "./byte" // for gsl::impl::byte #include "./span_ext" // for span specialization of gsl::at and other span-related extensions #include "./util" // for narrow_cast @@ -824,28 +824,28 @@ namespace details // [span.objectrep], views of object representation template -span::value> +span::value> as_bytes(span s) noexcept { - using type = span::value>; + using type = span::value>; // clang-format off GSL_SUPPRESS(type.1) // NO-FORMAT: attribute // clang-format on - return type{reinterpret_cast(s.data()), s.size_bytes()}; + return type{reinterpret_cast(s.data()), s.size_bytes()}; } template ::value, int> = 0> -span::value> +span::value> as_writable_bytes(span s) noexcept { - using type = span::value>; + using type = span::value>; // clang-format off GSL_SUPPRESS(type.1) // NO-FORMAT: attribute // clang-format on - return type{reinterpret_cast(s.data()), s.size_bytes()}; + return type{reinterpret_cast(s.data()), s.size_bytes()}; } } // namespace gsl