mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:37:46 +00:00
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:
parent
68cf22d580
commit
7036a9b6f7
2 changed files with 13 additions and 5 deletions
10
AK/Span.h
10
AK/Span.h
|
@ -52,11 +52,6 @@ public:
|
||||||
, m_size(other.m_size)
|
, 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 const T* data() const { return m_values; }
|
||||||
ALWAYS_INLINE T* data() { return m_values; }
|
ALWAYS_INLINE T* data() { return m_values; }
|
||||||
|
@ -115,6 +110,11 @@ public:
|
||||||
m_values = other.m_values;
|
m_values = other.m_values;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ALWAYS_INLINE operator Span<const T>() const
|
||||||
|
{
|
||||||
|
return { data(), size() };
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
T* m_values { nullptr };
|
T* m_values { nullptr };
|
||||||
size_t m_size { 0 };
|
size_t m_size { 0 };
|
||||||
|
|
|
@ -36,6 +36,14 @@ TEST_CASE(default_constructor_is_empty)
|
||||||
EXPECT(span.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)
|
TEST_CASE(span_works_with_constant_types)
|
||||||
{
|
{
|
||||||
const u8 buffer[4] { 1, 2, 3, 4 };
|
const u8 buffer[4] { 1, 2, 3, 4 };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue