warning: comparison between signed and unsigned integer expressions

3,452 views
Skip to first unread message

Mike

unread,
Oct 14, 2008, 10:12:43 PM10/14/08
to Google C++ Testing Framework
When compiling a file using google-test with gcc/g++ 4.2.3 on Ubuntu,
the following code:

std::vector a;
EXPECT_EQ(0, a.size();

results in this warning (with -Wall gcc flag set):

gtest/gtest.h:651: instantiated from ‘static
testing::AssertionResult
testing::internal::EqHelper<lhs_is_null_literal>::Compare(const char*,
const char*, const T1&, const T2&) [with T1 = int, T2 = size_t, bool
lhs_is_null_literal = false]’
gtest/gtest.h:618: warning: comparison between signed and unsigned
integer expressions


I can work around the warning by casting one of the values, such as

std::vector a;
EXPECT_EQ(size_t(0), a.size();

This is not ideal -- is there any cleaner way to get rid of the signed/
unsigned warning?

Vlad Losev

unread,
Oct 15, 2008, 12:54:48 AM10/15/08
to Mike, Google C++ Testing Framework
Hi Mike,

In short, there is no good way to make this warning go away. This is due to the way Google Test comparison macros are built. You can do one of the following things:

  * Explicitly cast your expected value to size_t, as you did.
  * Use unsigned constant for your expected value:
     EXPECT_EQ(0u, a.size());
  * Define a typed constant: const size_t Zero = 0;
    and compare agains it.
  * Disable the signed/unsigned mismatch warnings using
    -Wno-sign-compare option.
  * Use EXPECT_TRUE(a.size() == 0);

Each of these methods has its drawbacks, you have to choose the one that is the least evil for you.

Regards,
Vlad.
Reply all
Reply to author
Forward
0 new messages