From 831e5ed4e2291dcafb4ea221c5940d470233b637 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sun, 11 Aug 2024 09:38:23 -0400 Subject: [PATCH] AK: Allow comparing spans of different constness Otherwise, the following code would not compile: constexpr Array array { 4, 5, 6 }; Vector 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. --- AK/Span.h | 6 ++++++ Tests/AK/TestSpan.cpp | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/AK/Span.h b/AK/Span.h index de72faf0c9a..544e8c0ce4a 100644 --- a/AK/Span.h +++ b/AK/Span.h @@ -296,6 +296,12 @@ public: return TypedTransfer::compare(data(), other.data(), size()); } + constexpr bool operator==(Span const& other) const + requires(!IsConst) + { + return Span(*this) == other; + } + ALWAYS_INLINE constexpr operator ReadonlySpan() const { return { data(), size() }; diff --git a/Tests/AK/TestSpan.cpp b/Tests/AK/TestSpan.cpp index f9518d7706e..407f8ca407a 100644 --- a/Tests/AK/TestSpan.cpp +++ b/Tests/AK/TestSpan.cpp @@ -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 array { 4, 5, 6 }; + Vector vector { 4, 5, 6 }; + + EXPECT_EQ(array, vector.span()); +}