AK: Allow comparing spans of different constness

Otherwise, the following code would not compile:

    constexpr Array<int, 3> array { 4, 5, 6 };
    Vector<int> vector { 4, 5, 6 };

    if (array == vector.span()) { }

We do such comparisons in tests quite a bit. But it currently doesn't
become an issue because of the way EXPECT_EQ copies its input parameters
to non-const locals. In a future patch, that copying will be removed,
and the compiler would otherwise complain about not finding a suitable
comparison operator.
This commit is contained in:
Timothy Flynn 2024-08-11 09:38:23 -04:00 committed by Andreas Kling
parent c9caa4262e
commit 831e5ed4e2
Notes: github-actions[bot] 2024-08-13 12:14:06 +00:00
2 changed files with 14 additions and 0 deletions

View file

@ -296,6 +296,12 @@ public:
return TypedTransfer<T>::compare(data(), other.data(), size());
}
constexpr bool operator==(Span<T const> const& other) const
requires(!IsConst<T>)
{
return Span<T const>(*this) == other;
}
ALWAYS_INLINE constexpr operator ReadonlySpan<T>() const
{
return { data(), size() };

View file

@ -159,3 +159,11 @@ TEST_CASE(contains_slow)
EXPECT(!span.contains_slow(String {}));
EXPECT(!span.contains_slow(StringView {}));
}
TEST_CASE(compare_different_constness)
{
constexpr Array<int, 3> array { 4, 5, 6 };
Vector<int> vector { 4, 5, 6 };
EXPECT_EQ(array, vector.span());
}