Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler problem with operator and include catch_all.hpp #2792

Closed
rsc-vitronic opened this issue Jan 9, 2024 · 9 comments
Closed

Compiler problem with operator and include catch_all.hpp #2792

rsc-vitronic opened this issue Jan 9, 2024 · 9 comments

Comments

@rsc-vitronic
Copy link

rsc-vitronic commented Jan 9, 2024

Description
There is a compiler error, when #include <catch2/catch_all.hpp> is included.

Error msg is: C2039: "value": is not an element of "Catch::Detail::is_eq_comparable<LhsT,std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>,void>"

Simple example when the error occurs:

#include <catch2/catch_all.hpp>

TEST_CASE("first") {
  REQUIRE(true);
}

Using the include catch2/catch_test_macros.hpp instead of catch2/catch_all.hpp all is fine

Reproduction steps
Including catch2/catch_all.hpp in one of the catch internal tests (e.g. String.tests.cpp) reproduce this error too.

Platform information:

  • OS: Windows 10
  • Compiler+version: Visual Studio 2017 15.9.16
  • SDK-Version: 10.0.17763.0
  • Catch version: v3.5.0 and v3.5.1

Using v3.4.0 all is fine.

@rsc-vitronic
Copy link
Author

rsc-vitronic commented Jan 9, 2024

The error can be reproduced,

#include <catch2/catch_test_macros.hpp>



#include <catch2/benchmark/catch_benchmark_all.hpp>
#include <catch2/catch_approx.hpp>
#include <catch2/catch_assertion_info.hpp>
#include <catch2/catch_assertion_result.hpp>
#include <catch2/catch_config.hpp>
#include <catch2/catch_get_random_seed.hpp>
#include <catch2/catch_message.hpp>
#include <catch2/catch_section_info.hpp>
#include <catch2/catch_session.hpp>
#include <catch2/catch_tag_alias.hpp>
#include <catch2/catch_tag_alias_autoregistrar.hpp>
#include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_case_info.hpp>
#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_test_spec.hpp>
#include <catch2/catch_timer.hpp>
#include <catch2/catch_tostring.hpp>
#include <catch2/catch_totals.hpp>
#include <catch2/catch_translate_exception.hpp>
#include <catch2/catch_version.hpp>
#include <catch2/catch_version_macros.hpp>


// #include <catch2/generators/catch_generators_all.hpp>
#include <catch2/generators/catch_generator_exception.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <catch2/generators/catch_generators_adapters.hpp>
// adding this include creates this error
#include <catch2/generators/catch_generators_random.hpp>
#include <catch2/generators/catch_generators_range.hpp>


TEST_CASE("first") {
	REQUIRE(true);
	//REQUIRE(5 <= 5);
}

In the combination of the above includes (as they are from catch_all) the error can be "tuned on / off" by include catch_generators_random.hpp

Catch version: v3.5.0

@ChrisThrasher
Copy link
Collaborator

Can you reproduce this with a newer version of Visual Studio or does it only appear with VS 2017?

@rsc-vitronic
Copy link
Author

rsc-vitronic commented Jan 16, 2024

I'v just tried VS 2017 till now.

But with the new Catch version: v3.5.2 I've get the Error C2039 also in the SelfTest of
RandomNumberGeneration.tests.cpp

TEST_CASE("Comparison ops", "[rng]") {
    using Catch::SimplePcg32;
    REQUIRE(SimplePcg32{} == SimplePcg32{}); // C2039:  "value": is not an element of "Catch::Detail::is_eq_comparable<LhsT,Catch::SimplePcg32,void>"	SelfTest	...\14.16.27023\include\type_traits	74	

When I reduce the header include to the following in RandomNumberGeneration.tests.cpp and remove the rest of code, the Code compiles. Including catch_random_integer_helpers.hpp will create the error C2039:

#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_template_test_macros.hpp>
#include <catch2/internal/catch_floating_point_helpers.hpp>
// #include <catch2/internal/catch_random_integer_helpers.hpp>
#include <catch2/internal/catch_random_number_generator.hpp>
// #include <catch2/internal/catch_random_seed_generation.hpp>
// #include <catch2/internal/catch_uniform_floating_point_distribution.hpp>
// #include <catch2/internal/catch_uniform_integer_distribution.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <catch2/matchers/catch_matchers_range_equals.hpp>

TEST_CASE("Comparison ops", "[rng]") {
    using Catch::SimplePcg32;
    REQUIRE(SimplePcg32{} == SimplePcg32{});
    REQUIRE(SimplePcg32{ 0 } != SimplePcg32{});
    REQUIRE_FALSE(SimplePcg32{ 1 } == SimplePcg32{ 2 });
    REQUIRE_FALSE(SimplePcg32{ 1 } != SimplePcg32{ 1 });
}

Catch version: v3.5.2

@rsc-vitronic
Copy link
Author

Can you reproduce this with a newer version of Visual Studio or does it only appear with VS 2017?

Verified with the SelfTest of Catch v3.5.2 - Checked with the other compiler Visual Studio locally:
Visual Studio 2019 (v142) SDK Version used 10.0.10240.0 - SUCCESS
Visual Studio 2022 (v143) SDK Version used 10.0.10240.0 - SUCCESS
Visual Studio 2017 (v141) SDK Version used 10.0.10240.0 - FAILED

The results for Visual Studio 2019 & Visual Studio 2022 where expected, since they are part of the Windows builds windows-simple-builds.yml.

Question is, why is Visual Studio 2017 v141 not part of this test-builds. Is Visual Studio 2017not official supported any more?

Catch version: v3.5.2

@horenmar
Copy link
Member

2017 is currently not supported. The support policy is C++14 + maybe reasonable workarounds. Since I haven't used 2017 since, ..., well, ever, I am not interested in figuring out a workaround for VS2017 to accept the new RNG code.

If you are interested in figuring it out, I can point you to catch_random_integer_helpers.hpp, which trigger the bug.

@rsc-vitronic
Copy link
Author

Hi horenmar,
thanks for your replay. I can understand that not all compilers with their peculiarities can be supported.
May be it would be helpfull to define the supported compiler on the main project page.

@rsc-vitronic
Copy link
Author

rsc-vitronic commented Feb 18, 2024

In the meantime I was able to narrow down the problem further. The compiler stumbles with the == operator of a template structure.

#include <catch2/catch_test_macros.hpp>

namespace Catch {
    namespace Detail {
        template <typename T>
        struct MyTemplate {
            friend bool operator==( MyTemplate const& lhs,MyTemplate const& rhs ) { return false; }
        };
    } // namespace Detail
} // namespace Catch

namespace Catch {
    struct MyClass {
        friend bool operator==( MyClass const& lhs, MyClass const& rhs ) { return false; }
    };
} // namespace Catch

TEST_CASE( "first", "[x]" ) {
    Catch::Detail::MyTemplate<std::uint64_t> aa; // comment this line to get rid of the error
    CHECK_FALSE( std::string( "Hallo" ) == std::string( "huhu" ) );
    Catch::MyClass a, b;
    CHECK_FALSE( a == b ); // will generate compile error, when aa is defined
}

Then comes CATCH_INTERNAL_DEFINE_EXPRESSION_EQUALITY_OPERATOR( eq, == )
in stumble.
I couldn't solve the problem straight away. I'm not sure how to point the compiler in the right direction.

A simple but unpleasant solution is to put the template struct/class in its own namespace, such as Catch::Detail::Internal.

Puting all the code of the catch_random_integer_helpers.hpp in an inner namespace solved the problem and the code compiled and was usable. But I think that is not the desired strategy.

How about making the new RNG code deactivable using the CMAKE option? That would also mean Catch2 is usable with Visual Studio 2017.

@horenmar
Copy link
Member

Actually it looks like making the operator not-friend is sufficient: https://godbolt.org/z/783xW6jT5

@horenmar
Copy link
Member

(I would be willing to merge that PR)

rsc-vitronic pushed a commit to rsc-vitronic/Catch2 that referenced this issue Feb 22, 2024
…end operator == (catchorg#2792).

Make the operator not-friend is a sufficient solution.
rsc-vitronic pushed a commit to rsc-vitronic/Catch2 that referenced this issue Feb 22, 2024
…end operator == (catchorg#2792).

Make the operator not-friend is a sufficient solution. Closes catchorg#2792
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants