deprecate features adopted into C++ (#1198)

* deprecated features adopted into C++

1) Mark the following GSL features as deprecated:
 - gsl::unique_ptr (always)
 - gsl::shared_ptr (always)
 - gsl::byte (since c++17)
 - gsl::joining_thread (never implemented)
2) Refactor existing deprecations to use the new GSL_DEPRECATED(msg) macro.
3) Create a section in the README for deprecated features in the
   standard.

* do not deprecate gsl::to_integer because we never claim to implement it.

* do not use gsl::byte if it is deprecated
This commit is contained in:
Carson Radtke
2025-02-14 11:24:51 -06:00
committed by GitHub
parent 4742bc192a
commit 7fabaa499d
6 changed files with 64 additions and 20 deletions
+15 -5
View File
@@ -17,6 +17,8 @@
#ifndef GSL_BYTE_H
#define GSL_BYTE_H
#include "./util" // for GSL_DEPRECATED
#include <type_traits>
#ifdef _MSC_VER
@@ -72,6 +74,12 @@
#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 <cstddef>
#endif
@@ -80,7 +88,8 @@ namespace gsl
{
#if GSL_USE_STD_BYTE
using std::byte;
using byte GSL_DEPRECATED("Use std::byte instead.") = std::byte;
using std::to_integer;
#else // GSL_USE_STD_BYTE
@@ -155,23 +164,24 @@ constexpr IntegerType to_integer(byte b) noexcept
#endif // GSL_USE_STD_BYTE
template <typename T>
// 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 to_byte(T t) noexcept
constexpr BYTE_TYPE to_byte(T t) noexcept
{
static_assert(std::is_same<T, unsigned char>::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<t>() version.");
return byte(t);
return BYTE_TYPE(t);
}
template <int I>
constexpr byte to_byte() noexcept
constexpr BYTE_TYPE 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<byte>(I);
return static_cast<BYTE_TYPE>(I);
}
} // namespace gsl
+6 -2
View File
@@ -18,6 +18,7 @@
#define GSL_POINTERS_H
#include "./assert" // for Ensures, Expects
#include "./util" // for GSL_DEPRECATED
#include <cstddef> // for ptrdiff_t, nullptr_t, size_t
#include <functional> // for less, greater
@@ -62,8 +63,11 @@ namespace details
//
// GSL.owner: ownership pointers
//
using std::shared_ptr;
using std::unique_ptr;
template <typename... Ts>
using shared_ptr GSL_DEPRECATED("Use std::shared_ptr instead") = std::shared_ptr<Ts...>;
template <typename... Ts>
using unique_ptr GSL_DEPRECATED("Use std::unique_ptr instead") = std::unique_ptr<Ts...>;
//
// owner
+7 -7
View File
@@ -18,7 +18,7 @@
#define GSL_SPAN_H
#include "./assert" // for Expects
#include "./byte" // for byte
#include "./byte" // for BYTE_TYPE
#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 <class ElementType, std::size_t Extent>
span<const byte, details::calculate_byte_size<ElementType, Extent>::value>
span<const BYTE_TYPE, details::calculate_byte_size<ElementType, Extent>::value>
as_bytes(span<ElementType, Extent> s) noexcept
{
using type = span<const byte, details::calculate_byte_size<ElementType, Extent>::value>;
using type = span<const BYTE_TYPE, details::calculate_byte_size<ElementType, Extent>::value>;
// clang-format off
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
// clang-format on
return type{reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
return type{reinterpret_cast<const BYTE_TYPE*>(s.data()), s.size_bytes()};
}
template <class ElementType, std::size_t Extent,
std::enable_if_t<!std::is_const<ElementType>::value, int> = 0>
span<byte, details::calculate_byte_size<ElementType, Extent>::value>
span<BYTE_TYPE, details::calculate_byte_size<ElementType, Extent>::value>
as_writable_bytes(span<ElementType, Extent> s) noexcept
{
using type = span<byte, details::calculate_byte_size<ElementType, Extent>::value>;
using type = span<BYTE_TYPE, details::calculate_byte_size<ElementType, Extent>::value>;
// clang-format off
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
// clang-format on
return type{reinterpret_cast<byte*>(s.data()), s.size_bytes()};
return type{reinterpret_cast<BYTE_TYPE*>(s.data()), s.size_bytes()};
}
} // namespace gsl
+2 -2
View File
@@ -123,14 +123,14 @@ constexpr span<const typename Container::value_type> make_span(const Container&
}
template <class Ptr>
[[deprecated("This function is deprecated. See GSL issue #1092.")]]
GSL_DEPRECATED("This function is deprecated. See GSL issue #1092.")
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>
[[deprecated("This function is deprecated. See GSL issue #1092.")]]
GSL_DEPRECATED("This function is deprecated. See GSL issue #1092.")
constexpr span<typename Ptr::element_type> make_span(Ptr& cont)
{
return span<typename Ptr::element_type>(cont);
+26
View File
@@ -60,6 +60,32 @@
#define GSL_INLINE
#endif
#if defined(__has_cpp_attribute)
#if __has_cpp_attribute(deprecated)
#define GSL_DEPRECATED(msg) [[deprecated(msg)]]
#endif // __has_cpp_attribute(deprecated)
#endif // defined(__has_cpp_attribute)
#if !defined(GSL_DEPRECATED)
#if defined(__cplusplus)
#if __cplusplus >= 201309L
#define GSL_DEPRECATED(msg) [[deprecated(msg)]]
#endif // __cplusplus >= 201309L
#endif // defined(__cplusplus)
#endif // !defined(GSL_DEPRECATED)
#if !defined(GSL_DEPRECATED)
#if defined(_MSC_VER)
#define GSL_DEPRECATED(msg) __declspec(deprecated(msg))
#elif defined(__GNUC__)
#define GSL_DEPRECATED(msg) __attribute__((deprecated(msg)))
#endif // defined(_MSC_VER)
#endif // !defined(GSL_DEPRECATED)
#if !defined(GSL_DEPRECATED)
#define GSL_DEPRECATED(msg)
#endif // !defined(GSL_DEPRECATED)
namespace gsl
{
//