From 5de7d757a8a56249965cd724e0da26a9818948fc Mon Sep 17 00:00:00 2001 From: sha512sum Date: Tue, 27 Feb 2024 22:19:19 +0000 Subject: [PATCH] Custom operator== in GoInterface now takes precedence --- include/utempl/go_interface.hpp | 5 +++++ tests/go_interface.cpp | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/utempl/go_interface.hpp b/include/utempl/go_interface.hpp index a3d7911..11c3301 100644 --- a/include/utempl/go_interface.hpp +++ b/include/utempl/go_interface.hpp @@ -85,6 +85,11 @@ struct GoInterface : Value { transformer(impl::TryGet().size() + 1>{boost::pfr::get_name().begin()}>(other))) && ...); }(std::make_index_sequence>()); }; + template + inline constexpr auto operator==(T&& other) const -> bool + requires (requires(const Value& value){value == other;}) { + return static_cast(*this) == other; + }; }; } // namespace utempl diff --git a/tests/go_interface.cpp b/tests/go_interface.cpp index 7d5a317..b1f0436 100644 --- a/tests/go_interface.cpp +++ b/tests/go_interface.cpp @@ -7,6 +7,13 @@ struct SomeInterface { struct SomeStruct { int field; + friend auto operator==(const SomeInterface& a, const SomeStruct& b) -> bool { + return false; + }; +}; + +struct SomeStruct2 { + int field; }; TEST(GoInterface, Basic) { @@ -18,5 +25,6 @@ TEST(GoInterface, Equal) { utempl::GoInterface obj(SomeStruct{1}); EXPECT_EQ(obj, utempl::GoInterface{SomeInterface{1}}); EXPECT_EQ(obj, SomeInterface{1}); - EXPECT_EQ(obj, SomeStruct{1}); + EXPECT_NE(obj, SomeStruct{1}); + EXPECT_EQ(obj, SomeStruct2{1}); };