googletest icon indicating copy to clipboard operation
googletest copied to clipboard

[Bug]: FieldsAre cannot support struct type

Open Kevin-WongCHN opened this issue 1 year ago • 3 comments
trafficstars

Describe the issue

the documentation about FieldsAre wrote:

struct MyStruct {
  int value = 42;
  std::string greeting = "aloha";
};
MyStruct s;
EXPECT_THAT(s, FieldsAre(42, "aloha"));

however, when I run this:

struct MyStruct {
  int value = 42;
  std::string greeting = "aloha";
};
TEST(MyTestSuite, FieldsAreMatcherTest) {
    MyStruct s;
    EXPECT_THAT(s, FieldsAre(42, "aloha"));
}

g++ cannot compile: note: types 'std::pair<_Tp1, _Tp2>' and 'const MyStruct' have incompatible cv-qualifiers

Steps to reproduce the problem

wrote the above code and run googletest

What version of GoogleTest are you using?

1.15.2

What operating system and version are you using?

windows10

What compiler and version are you using?

gcc (tdm64-1) 9.2.0

What build system are you using?

cmake version 3.22.2

Additional context

No response

Kevin-WongCHN avatar Nov 14 '24 08:11 Kevin-WongCHN

@Kevin-WongCHN this seams to be a challenge with the compiler version and gets resolved in gcc 11.1 and later. You may also check the reference here.

fynnwilliam avatar Nov 19 '24 15:11 fynnwilliam

What C++ version are you using? Docs clearly say that use for structs requires structured bindings which came in C++17. I don't know what GCC 9.2.0 defaults to, but it sounds a bit old as it came out in 2019.

dkaszews avatar Feb 06 '25 17:02 dkaszews

I am almost sure that is the case, because @fynnwilliam 's godbolt link breaks if you switch to GCC 9.2, but then works again if you add -std=c++17 to compile flags.

dkaszews avatar Feb 06 '25 17:02 dkaszews