AK: Define conversion from Span<T> to Span<const T> correctly.

I accidently wrote `Span<RemoveConst<T>>` when I meant
`Span<RemoveConst<T>::Type>`.

Changing that wouldn't be enough though, this constructor can only be
defined if T is not const, otherwise it would redefine the copy
constructor.  This can be avoided by overloading the cast operator.
This commit is contained in:
asynts 2020-07-26 18:08:40 +02:00 committed by Andreas Kling
parent 68cf22d580
commit 7036a9b6f7
Notes: sideshowbarker 2024-07-19 04:33:04 +09:00
2 changed files with 13 additions and 5 deletions

View file

@ -52,11 +52,6 @@ public:
, m_size(other.m_size)
{
}
ALWAYS_INLINE Span(const Span<RemoveConst<T>>& other)
: m_values(other.m_values)
, m_size(other.m_size)
{
}
ALWAYS_INLINE const T* data() const { return m_values; }
ALWAYS_INLINE T* data() { return m_values; }
@ -115,6 +110,11 @@ public:
m_values = other.m_values;
}
ALWAYS_INLINE operator Span<const T>() const
{
return { data(), size() };
}
protected:
T* m_values { nullptr };
size_t m_size { 0 };

View file

@ -36,6 +36,14 @@ TEST_CASE(default_constructor_is_empty)
EXPECT(span.is_empty());
}
TEST_CASE(implicit_converson_to_const)
{
Bytes bytes0;
ReadonlyBytes bytes1 { bytes0 };
[[maybe_unused]] ReadonlyBytes bytes2 = bytes0;
[[maybe_unused]] ReadonlyBytes bytes3 = static_cast<ReadonlyBytes>(bytes2);
}
TEST_CASE(span_works_with_constant_types)
{
const u8 buffer[4] { 1, 2, 3, 4 };