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
Notes:
sideshowbarker
2024-07-19 04:33:04 +09:00
Author: https://github.com/asynts Commit: https://github.com/SerenityOS/serenity/commit/7036a9b6f7d Pull-request: https://github.com/SerenityOS/serenity/pull/2903
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)
|
||||
{
|
||||
}
|
||||
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 };
|
||||
|
|
|
@ -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 };
|
||||
|
|
Loading…
Add table
Reference in a new issue