diff --git a/src/catch2/internal/catch_decomposer.hpp b/src/catch2/internal/catch_decomposer.hpp index 9c004b0298..62220e9015 100644 --- a/src/catch2/internal/catch_decomposer.hpp +++ b/src/catch2/internal/catch_decomposer.hpp @@ -132,6 +132,29 @@ namespace Catch { // This was added in C++20, but we require only C++14 for now. template using RemoveCVRef_t = std::remove_cv_t>; + + // Perform ordinary lookup before decomposition operators can hide + // global user-defined comparisons. Decomposition uses lvalues. +#define CATCH_INTERNAL_COMPARISON_HELPERS(id, op) \ + template \ + constexpr bool compare_##id(L& lhs, R& rhs) { \ + return static_cast(lhs op rhs); \ + } \ + template \ + constexpr bool compare_##id##_zero(T& lhs) { \ + return static_cast(lhs op 0); \ + } \ + template \ + constexpr bool compare_zero_##id(T& rhs) { \ + return static_cast(0 op rhs); \ + } + CATCH_INTERNAL_COMPARISON_HELPERS(eq, ==) + CATCH_INTERNAL_COMPARISON_HELPERS(ne, !=) + CATCH_INTERNAL_COMPARISON_HELPERS(lt, <) + CATCH_INTERNAL_COMPARISON_HELPERS(le, <=) + CATCH_INTERNAL_COMPARISON_HELPERS(gt, >) + CATCH_INTERNAL_COMPARISON_HELPERS(ge, >=) +#undef CATCH_INTERNAL_COMPARISON_HELPERS } // Note: This is about as much as we can currently reasonably support. @@ -294,7 +317,7 @@ namespace Catch { Detail::negation>>>::value, \ BinaryExpr> { \ - return { static_cast( lhs.getLhs() op rhs ), \ + return { Detail::compare_##id( lhs.getLhs(), rhs ), \ lhs.getLhs(), \ #op##_sr, \ rhs }; \ @@ -305,7 +328,7 @@ namespace Catch { Detail::conjunction, \ capture_by_value>::value, \ BinaryExpr> { \ - return { static_cast( lhs.getLhs() op rhs ), \ + return { Detail::compare_##id( lhs.getLhs(), rhs ), \ lhs.getLhs(), \ #op##_sr, \ rhs }; \ @@ -322,7 +345,7 @@ namespace Catch { std::is_same>>::value, \ BinaryExpr> { \ if ( rhs != 0 ) { throw_test_failure_exception(); } \ - return { static_cast( lhs.getLhs() op 0 ), \ + return { Detail::compare_##id##_zero( lhs.getLhs() ), \ lhs.getLhs(), \ #op##_sr, \ rhs }; \ @@ -339,7 +362,7 @@ namespace Catch { std::is_same>>::value, \ BinaryExpr> { \ if ( lhs.getLhs() != 0 ) { throw_test_failure_exception(); } \ - return { static_cast( 0 op rhs ), lhs.getLhs(), #op##_sr, rhs }; \ + return { Detail::compare_zero_##id( rhs ), lhs.getLhs(), #op##_sr, rhs }; \ } CATCH_INTERNAL_DEFINE_EXPRESSION_EQUALITY_OPERATOR( eq, == ) @@ -355,7 +378,7 @@ namespace Catch { Detail::negation>>>::value, \ BinaryExpr> { \ - return { static_cast( lhs.getLhs() op rhs ), \ + return { Detail::compare_##id( lhs.getLhs(), rhs ), \ lhs.getLhs(), \ #op##_sr, \ rhs }; \ @@ -366,7 +389,7 @@ namespace Catch { Detail::conjunction, \ capture_by_value>::value, \ BinaryExpr> { \ - return { static_cast( lhs.getLhs() op rhs ), \ + return { Detail::compare_##id( lhs.getLhs(), rhs ), \ lhs.getLhs(), \ #op##_sr, \ rhs }; \ @@ -380,7 +403,7 @@ namespace Catch { std::is_same>::value, \ BinaryExpr> { \ if ( rhs != 0 ) { throw_test_failure_exception(); } \ - return { static_cast( lhs.getLhs() op 0 ), \ + return { Detail::compare_##id##_zero( lhs.getLhs() ), \ lhs.getLhs(), \ #op##_sr, \ rhs }; \ @@ -394,7 +417,7 @@ namespace Catch { std::is_same>::value, \ BinaryExpr> { \ if ( lhs.getLhs() != 0 ) { throw_test_failure_exception(); } \ - return { static_cast( 0 op rhs ), lhs.getLhs(), #op##_sr, rhs }; \ + return { Detail::compare_zero_##id( rhs ), lhs.getLhs(), #op##_sr, rhs }; \ } CATCH_INTERNAL_DEFINE_EXPRESSION_COMPARISON_OPERATOR( lt, < ) diff --git a/tests/SelfTest/UsageTests/Decomposition.tests.cpp b/tests/SelfTest/UsageTests/Decomposition.tests.cpp index e92f740606..adbce4201d 100644 --- a/tests/SelfTest/UsageTests/Decomposition.tests.cpp +++ b/tests/SelfTest/UsageTests/Decomposition.tests.cpp @@ -8,6 +8,39 @@ #include #include +#include + +namespace regression_3221 { + struct Value { int value; }; + struct NullValue {}; +} + +// These deliberately live outside Value's namespace, before Catch2 headers. +#define DEFINE_GLOBAL_COMPARISON(op) \ + constexpr bool operator op(regression_3221::Value const& lhs, \ + regression_3221::Value const& rhs) { \ + return lhs.value op rhs.value; \ + } +DEFINE_GLOBAL_COMPARISON(==) +DEFINE_GLOBAL_COMPARISON(!=) +DEFINE_GLOBAL_COMPARISON(<) +DEFINE_GLOBAL_COMPARISON(<=) +DEFINE_GLOBAL_COMPARISON(>) +DEFINE_GLOBAL_COMPARISON(>=) +#undef DEFINE_GLOBAL_COMPARISON + +constexpr bool operator==(regression_3221::NullValue const&, std::nullptr_t) { + return true; +} +constexpr bool operator==(std::nullptr_t, regression_3221::NullValue const&) { + return true; +} +constexpr bool operator!=(regression_3221::NullValue const&, std::nullptr_t) { + return false; +} +constexpr bool operator!=(std::nullptr_t, regression_3221::NullValue const&) { + return false; +} namespace { @@ -28,6 +61,30 @@ std::ostream& operator<<(std::ostream& o, truthy) { #include +TEST_CASE("Global comparisons declared before Catch2 headers: #3221", + "[Decomposition][approvals]") { + regression_3221::Value const one{1}, two{2}; + REQUIRE(one == one); + REQUIRE_FALSE(one == two); + REQUIRE(one != two); + REQUIRE_FALSE(one != one); + REQUIRE(one < two); + REQUIRE(one <= one); + REQUIRE(two > one); + REQUIRE(two >= two); + REQUIRE(regression_3221::Value{1} == regression_3221::Value{1}); + REQUIRE(one != regression_3221::Value{2}); + regression_3221::NullValue const null; + REQUIRE(null == 0); + REQUIRE(0 == null); + REQUIRE(null == 0L); + REQUIRE(0L == null); + REQUIRE_FALSE(null != 0); + REQUIRE_FALSE(0 != null); + REQUIRE_FALSE(null != 0L); + REQUIRE_FALSE(0L != null); +} + TEST_CASE( "Reconstruction should be based on stringification: #914" , "[Decomposition][failing][.]") { CHECK(truthy(false)); }