refactor: Remove unused Span alias

This commit is contained in:
MarcoFalke 2024-12-18 16:42:30 +01:00
parent 78be7cdec4
commit 2087247702

View file

@ -11,38 +11,38 @@
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
/** A Span is an object that can refer to a contiguous sequence of objects. /** A std::span is an object that can refer to a contiguous sequence of objects.
* *
* Things to be aware of when writing code that deals with Spans: * Things to be aware of when writing code that deals with std::span objectss:
* *
* - Similar to references themselves, Spans are subject to reference lifetime * - Similar to references themselves, std::span objects are subject to reference lifetime
* issues. The user is responsible for making sure the objects pointed to by * issues. The user is responsible for making sure the objects pointed to by
* a Span live as long as the Span is used. For example: * a std::span live as long as the std::span is used. For example:
* *
* std::vector<int> vec{1,2,3,4}; * std::vector<int> vec{1,2,3,4};
* Span<int> sp(vec); * std::span<int> sp(vec);
* vec.push_back(5); * vec.push_back(5);
* printf("%i\n", sp.front()); // UB! * printf("%i\n", sp.front()); // UB!
* *
* may exhibit undefined behavior, as increasing the size of a vector may * may exhibit undefined behavior, as increasing the size of a vector may
* invalidate references. * invalidate references.
* *
* - One particular pitfall is that Spans can be constructed from temporaries, * - One particular pitfall is that std::span objects can be constructed from temporaries,
* but this is unsafe when the Span is stored in a variable, outliving the * but this is unsafe when the std::span is stored in a variable, outliving the
* temporary. For example, this will compile, but exhibits undefined behavior: * temporary. For example, this will compile, but exhibits undefined behavior:
* *
* Span<const int> sp(std::vector<int>{1, 2, 3}); * std::span<const int> sp(std::vector<int>{1, 2, 3});
* printf("%i\n", sp.front()); // UB! * printf("%i\n", sp.front()); // UB!
* *
* The lifetime of the vector ends when the statement it is created in ends. * The lifetime of the vector ends when the statement it is created in ends.
* Thus the Span is left with a dangling reference, and using it is undefined. * Thus the std::span is left with a dangling reference, and using it is undefined.
* *
* - Due to Span's automatic creation from range-like objects (arrays, and data * - Due to std::span's automatic creation from range-like objects (arrays, and data
* types that expose a data() and size() member function), functions that * types that expose a data() and size() member function), functions that
* accept a Span as input parameter can be called with any compatible * accept a std::span as input parameter can be called with any compatible
* range-like object. For example, this works: * range-like object. For example, this works:
* *
* void Foo(Span<const int> arg); * void Foo(std::span<const int> arg);
* *
* Foo(std::vector<int>{1, 2, 3}); // Works * Foo(std::vector<int>{1, 2, 3}); // Works
* *
@ -50,10 +50,10 @@
* container, and only about having exactly a range of elements. However it * container, and only about having exactly a range of elements. However it
* may also be surprising to see automatic conversions in this case. * may also be surprising to see automatic conversions in this case.
* *
* When a function accepts a Span with a mutable element type, it will not * When a function accepts a std::span with a mutable element type, it will not
* accept temporaries; only variables or other references. For example: * accept temporaries; only variables or other references. For example:
* *
* void FooMut(Span<int> arg); * void FooMut(std::span<int> arg);
* *
* FooMut(std::vector<int>{1, 2, 3}); // Does not compile * FooMut(std::vector<int>{1, 2, 3}); // Does not compile
* std::vector<int> baz{1, 2, 3}; * std::vector<int> baz{1, 2, 3};
@ -69,7 +69,6 @@
* result will be present in that variable after the call. Passing a temporary * result will be present in that variable after the call. Passing a temporary
* is useless in that context. * is useless in that context.
*/ */
#define Span std::span
/** Pop the last element off a span, and return a reference to that element. */ /** Pop the last element off a span, and return a reference to that element. */
template <typename T> template <typename T>
@ -81,18 +80,6 @@ T& SpanPopBack(std::span<T>& span)
return back; return back;
} }
// From C++20 as_bytes and as_writeable_bytes
template <typename T>
Span<const std::byte> AsBytes(Span<T> s) noexcept
{
return {reinterpret_cast<const std::byte*>(s.data()), s.size_bytes()};
}
template <typename T>
Span<std::byte> AsWritableBytes(Span<T> s) noexcept
{
return {reinterpret_cast<std::byte*>(s.data()), s.size_bytes()};
}
template <typename V> template <typename V>
auto MakeByteSpan(const V& v) noexcept auto MakeByteSpan(const V& v) noexcept
{ {
@ -117,10 +104,10 @@ inline const unsigned char* UCharCast(const std::byte* c) { return reinterpret_c
template <typename B> template <typename B>
concept BasicByte = requires { UCharCast(std::span<B>{}.data()); }; concept BasicByte = requires { UCharCast(std::span<B>{}.data()); };
// Helper function to safely convert a Span to a Span<[const] unsigned char>. // Helper function to safely convert a std::span to a std::span<[const] unsigned char>.
template <typename T, size_t N> constexpr auto UCharSpanCast(std::span<T, N> s) -> std::span<typename std::remove_pointer_t<decltype(UCharCast(s.data()))>> { return {UCharCast(s.data()), s.size()}; } template <typename T, size_t N> constexpr auto UCharSpanCast(std::span<T, N> s) -> std::span<typename std::remove_pointer_t<decltype(UCharCast(s.data()))>> { return {UCharCast(s.data()), s.size()}; }
/** Like the Span constructor, but for (const) unsigned char member types only. Only works for (un)signed char containers. */ /** Like the std::span constructor, but for (const) unsigned char member types only. Only works for (un)signed char containers. */
template <typename V> constexpr auto MakeUCharSpan(const V& v) -> decltype(UCharSpanCast(std::span{v})) { return UCharSpanCast(std::span{v}); } template <typename V> constexpr auto MakeUCharSpan(const V& v) -> decltype(UCharSpanCast(std::span{v})) { return UCharSpanCast(std::span{v}); }
template <typename V> constexpr auto MakeWritableUCharSpan(V&& v) -> decltype(UCharSpanCast(std::span{std::forward<V>(v)})) { return UCharSpanCast(std::span{std::forward<V>(v)}); } template <typename V> constexpr auto MakeWritableUCharSpan(V&& v) -> decltype(UCharSpanCast(std::span{std::forward<V>(v)})) { return UCharSpanCast(std::span{std::forward<V>(v)}); }