Skip to content

Commit 7fabaa4

Browse files
authored
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
1 parent 4742bc1 commit 7fabaa4

File tree

6 files changed

+64
-20
lines changed

6 files changed

+64
-20
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,18 @@ span_p | &#x26
4040
[u32zstring](docs/headers.md#user-content-H-zstring) | ☑ | An alias to `basic_zstring` with dynamic extent and a char type of `char32_t`
4141
[cu32zstring](docs/headers.md#user-content-H-zstring) | ☑ | An alias to `basic_zstring` with dynamic extent and a char type of `const char32_t`
4242
[**2. Owners**][cg-owners] | |
43-
[unique_ptr](docs/headers.md#user-content-H-pointers-unique_ptr) | ☑ | An alias to `std::unique_ptr`
44-
[shared_ptr](docs/headers.md#user-content-H-pointers-shared_ptr) | ☑ | An alias to `std::shared_ptr`
4543
stack_array | ☐ | A stack-allocated array
4644
dyn_array | ☐ | A heap-allocated array
4745
[**3. Assertions**][cg-assertions] | |
4846
[Expects](docs/headers.md#user-content-H-assert-expects) | ☑ | A precondition assertion; on failure it terminates
4947
[Ensures](docs/headers.md#user-content-H-assert-ensures) | ☑ | A postcondition assertion; on failure it terminates
5048
[**4. Utilities**][cg-utilities] | |
5149
move_owner | ☐ | A helper function that moves one `owner` to the other
52-
[byte](docs/headers.md#user-content-H-byte-byte) | ☑ | Either an alias to `std::byte` or a byte type
5350
[final_action](docs/headers.md#user-content-H-util-final_action) | ☑ | A RAII style class that invokes a functor on its destruction
5451
[finally](docs/headers.md#user-content-H-util-finally) | ☑ | A helper function instantiating [final_action](docs/headers.md#user-content-H-util-final_action)
5552
[GSL_SUPPRESS](docs/headers.md#user-content-H-assert-gsl_suppress) | ☑ | A macro that takes an argument and turns it into `[[gsl::suppress(x)]]` or `[[gsl::suppress("x")]]`
5653
[[implicit]] | ☐ | A "marker" to put on single-argument constructors to explicitly make them non-explicit
5754
[index](docs/headers.md#user-content-H-util-index) | ☑ | A type to use for all container and array indexing (currently an alias for `std::ptrdiff_t`)
58-
joining_thread | ☐ | A RAII style version of `std::thread` that joins
5955
[narrow](docs/headers.md#user-content-H-narrow-narrow) | ☑ | A checked version of `narrow_cast`; it can throw [narrowing_error](docs/headers.md#user-content-H-narrow-narrowing_error)
6056
[narrow_cast](docs/headers.md#user-content-H-util-narrow_cast) | ☑ | A narrowing cast for values and a synonym for `static_cast`
6157
[narrowing_error](docs/headers.md#user-content-H-narrow-narrowing_error) | ☑ | A custom exception type thrown by [narrow](docs/headers.md#user-content-H-narrow-narrow)
@@ -77,6 +73,14 @@ cu16string_span | ☐ | Deprecated. An alias to `basic
7773
u32string_span | ☐ | Deprecated. An alias to `basic_string_span` with a char type of `char32_t`
7874
cu32string_span | ☐ | Deprecated. An alias to `basic_string_span` with a char type of `const char32_t`
7975

76+
## The following features have been adopted by WG21. They are deprecated in GSL.
77+
Feature | Deprecated Since | Notes
78+
-----------------------------------|------------------|------
79+
gsl::unique_ptr | C++11 | Use std::unique_ptr instead.
80+
gsl::shared_ptr | C++11 | Use std::shared_ptr instead.
81+
gsl::byte | C++17 | Use std::byte instead.
82+
gsl:joining_thread | C++20 (Note: Not yet implemented in GSL) | Use std::jthread instead.
83+
8084
This is based on [CppCoreGuidelines semi-specification](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#gsl-guidelines-support-library).
8185

8286
[cg-views]: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#gslview-views

include/gsl/byte

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#ifndef GSL_BYTE_H
1818
#define GSL_BYTE_H
1919

20+
#include "./util" // for GSL_DEPRECATED
21+
2022
#include <type_traits>
2123

2224
#ifdef _MSC_VER
@@ -72,6 +74,12 @@
7274
#define byte_may_alias
7375
#endif // defined __clang__ || defined __GNUC__
7476

77+
#if GSL_USE_STD_BYTE
78+
#define BYTE_TYPE std::byte
79+
#else // !GSL_USE_STD_BYTE
80+
#define BYTE_TYPE byte
81+
#endif // GSL_USE_STD_BYTE
82+
7583
#if GSL_USE_STD_BYTE
7684
#include <cstddef>
7785
#endif
@@ -80,7 +88,8 @@ namespace gsl
8088
{
8189
#if GSL_USE_STD_BYTE
8290

83-
using std::byte;
91+
using byte GSL_DEPRECATED("Use std::byte instead.") = std::byte;
92+
8493
using std::to_integer;
8594

8695
#else // GSL_USE_STD_BYTE
@@ -155,23 +164,24 @@ constexpr IntegerType to_integer(byte b) noexcept
155164

156165
#endif // GSL_USE_STD_BYTE
157166

167+
158168
template <typename T>
159169
// NOTE: need suppression since c++14 does not allow "return {t}"
160170
// GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: suppression does not work
161-
constexpr byte to_byte(T t) noexcept
171+
constexpr BYTE_TYPE to_byte(T t) noexcept
162172
{
163173
static_assert(std::is_same<T, unsigned char>::value,
164174
"gsl::to_byte(t) must be provided an unsigned char, otherwise data loss may occur. "
165175
"If you are calling to_byte with an integer constant use: gsl::to_byte<t>() version.");
166-
return byte(t);
176+
return BYTE_TYPE(t);
167177
}
168178

169179
template <int I>
170-
constexpr byte to_byte() noexcept
180+
constexpr BYTE_TYPE to_byte() noexcept
171181
{
172182
static_assert(I >= 0 && I <= 255,
173183
"gsl::byte only has 8 bits of storage, values must be in range 0-255");
174-
return static_cast<byte>(I);
184+
return static_cast<BYTE_TYPE>(I);
175185
}
176186

177187
} // namespace gsl

include/gsl/pointers

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define GSL_POINTERS_H
1919

2020
#include "./assert" // for Ensures, Expects
21+
#include "./util" // for GSL_DEPRECATED
2122

2223
#include <cstddef> // for ptrdiff_t, nullptr_t, size_t
2324
#include <functional> // for less, greater
@@ -62,8 +63,11 @@ namespace details
6263
//
6364
// GSL.owner: ownership pointers
6465
//
65-
using std::shared_ptr;
66-
using std::unique_ptr;
66+
template <typename... Ts>
67+
using shared_ptr GSL_DEPRECATED("Use std::shared_ptr instead") = std::shared_ptr<Ts...>;
68+
69+
template <typename... Ts>
70+
using unique_ptr GSL_DEPRECATED("Use std::unique_ptr instead") = std::unique_ptr<Ts...>;
6771

6872
//
6973
// owner

include/gsl/span

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#define GSL_SPAN_H
1919

2020
#include "./assert" // for Expects
21-
#include "./byte" // for byte
21+
#include "./byte" // for BYTE_TYPE
2222
#include "./span_ext" // for span specialization of gsl::at and other span-related extensions
2323
#include "./util" // for narrow_cast
2424

@@ -824,28 +824,28 @@ namespace details
824824

825825
// [span.objectrep], views of object representation
826826
template <class ElementType, std::size_t Extent>
827-
span<const byte, details::calculate_byte_size<ElementType, Extent>::value>
827+
span<const BYTE_TYPE, details::calculate_byte_size<ElementType, Extent>::value>
828828
as_bytes(span<ElementType, Extent> s) noexcept
829829
{
830-
using type = span<const byte, details::calculate_byte_size<ElementType, Extent>::value>;
830+
using type = span<const BYTE_TYPE, details::calculate_byte_size<ElementType, Extent>::value>;
831831

832832
// clang-format off
833833
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
834834
// clang-format on
835-
return type{reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
835+
return type{reinterpret_cast<const BYTE_TYPE*>(s.data()), s.size_bytes()};
836836
}
837837

838838
template <class ElementType, std::size_t Extent,
839839
std::enable_if_t<!std::is_const<ElementType>::value, int> = 0>
840-
span<byte, details::calculate_byte_size<ElementType, Extent>::value>
840+
span<BYTE_TYPE, details::calculate_byte_size<ElementType, Extent>::value>
841841
as_writable_bytes(span<ElementType, Extent> s) noexcept
842842
{
843-
using type = span<byte, details::calculate_byte_size<ElementType, Extent>::value>;
843+
using type = span<BYTE_TYPE, details::calculate_byte_size<ElementType, Extent>::value>;
844844

845845
// clang-format off
846846
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
847847
// clang-format on
848-
return type{reinterpret_cast<byte*>(s.data()), s.size_bytes()};
848+
return type{reinterpret_cast<BYTE_TYPE*>(s.data()), s.size_bytes()};
849849
}
850850

851851
} // namespace gsl

include/gsl/span_ext

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,14 @@ constexpr span<const typename Container::value_type> make_span(const Container&
123123
}
124124

125125
template <class Ptr>
126-
[[deprecated("This function is deprecated. See GSL issue #1092.")]]
126+
GSL_DEPRECATED("This function is deprecated. See GSL issue #1092.")
127127
constexpr span<typename Ptr::element_type> make_span(Ptr& cont, std::size_t count)
128128
{
129129
return span<typename Ptr::element_type>(cont, count);
130130
}
131131

132132
template <class Ptr>
133-
[[deprecated("This function is deprecated. See GSL issue #1092.")]]
133+
GSL_DEPRECATED("This function is deprecated. See GSL issue #1092.")
134134
constexpr span<typename Ptr::element_type> make_span(Ptr& cont)
135135
{
136136
return span<typename Ptr::element_type>(cont);

include/gsl/util

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,32 @@
6060
#define GSL_INLINE
6161
#endif
6262

63+
#if defined(__has_cpp_attribute)
64+
#if __has_cpp_attribute(deprecated)
65+
#define GSL_DEPRECATED(msg) [[deprecated(msg)]]
66+
#endif // __has_cpp_attribute(deprecated)
67+
#endif // defined(__has_cpp_attribute)
68+
69+
#if !defined(GSL_DEPRECATED)
70+
#if defined(__cplusplus)
71+
#if __cplusplus >= 201309L
72+
#define GSL_DEPRECATED(msg) [[deprecated(msg)]]
73+
#endif // __cplusplus >= 201309L
74+
#endif // defined(__cplusplus)
75+
#endif // !defined(GSL_DEPRECATED)
76+
77+
#if !defined(GSL_DEPRECATED)
78+
#if defined(_MSC_VER)
79+
#define GSL_DEPRECATED(msg) __declspec(deprecated(msg))
80+
#elif defined(__GNUC__)
81+
#define GSL_DEPRECATED(msg) __attribute__((deprecated(msg)))
82+
#endif // defined(_MSC_VER)
83+
#endif // !defined(GSL_DEPRECATED)
84+
85+
#if !defined(GSL_DEPRECATED)
86+
#define GSL_DEPRECATED(msg)
87+
#endif // !defined(GSL_DEPRECATED)
88+
6389
namespace gsl
6490
{
6591
//

0 commit comments

Comments
 (0)