Skip to content

Commit cd7bf6e

Browse files
authored
Merge pull request #436 from elbeno/shrunk-comparison
🎨 Make shrinking transparent to equality comparison
2 parents 73ee5a4 + ee1d18b commit cd7bf6e

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎include/stdx/type_traits.hpp‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,18 @@ namespace detail {
221221
template <typename T> struct shrinkwrap {
222222
using is_shrinkwrapped = void;
223223
using type = T;
224+
friend constexpr auto operator==(shrinkwrap const &, shrinkwrap const &)
225+
-> bool = default;
224226
};
225227

226228
template <auto V> struct shrinkwrap_value {
227229
using is_shrinkwrapped_value = void;
230+
using type = std::remove_cvref_t<decltype(V)>;
228231
constexpr static auto value = V;
232+
233+
friend constexpr auto operator==(shrinkwrap_value const &,
234+
shrinkwrap_value const &)
235+
-> bool = default;
229236
};
230237

231238
template <typename T>
@@ -268,9 +275,7 @@ template <auto V,
268275
consteval auto shrink_impl() {
269276
return F;
270277
}
271-
} // namespace stdx
272278

273-
namespace stdx {
274279
inline namespace v1 {
275280
namespace detail {
276281
template <typename T> consteval auto maybe_expand() -> T;
@@ -323,6 +328,13 @@ template <typename T> constexpr auto expand(T &&t) -> decltype(auto) {
323328
template <typename T> using shrink_t = decltype(shrink<T>());
324329
template <typename T> using expand_t = decltype(expand<T>());
325330

331+
template <detail::is_shrunk_value T, typename U>
332+
constexpr auto operator==(T const &t, U const &u) -> bool
333+
requires requires { expand(t) == u; }
334+
{
335+
return expand(t) == u;
336+
}
337+
326338
STDX_PRAGMA(diagnostic push)
327339
#ifdef __clang__
328340
STDX_PRAGMA(diagnostic ignored "-Wunknown-warning-option")

‎test/ct_string.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ TEST_CASE("named object with shrunk name", "[ct_string]") {
226226
STATIC_CHECK(stdx::matching_name_q<"test">::template fn<T>::value);
227227
}
228228

229+
TEST_CASE("shrunk ct_string compares equal to ct_string", "[ct_string]") {
230+
using namespace stdx::ct_string_literals;
231+
constexpr auto s = stdx::shrink<"test">();
232+
STATIC_CHECK(s == "test"_cts);
233+
STATIC_CHECK("test"_cts == s);
234+
STATIC_CHECK(s == s);
235+
}
236+
229237
TEST_CASE("name or type fallback", "[ct_string]") {
230238
using namespace stdx::ct_string_literals;
231239
using T = stdx::with_name<"test">;

0 commit comments

Comments
 (0)