Skip to content

Commit

Permalink
fix C4127: Conditional Expression is Constant
Browse files Browse the repository at this point in the history
  • Loading branch information
abeaucha committed Sep 18, 2024
1 parent 52d239f commit a9002b3
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions modules/c++/sys/unittests/test_NaN_testing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ TEST_CASE(testIsNaN)

TEST_CASE(test_ssize)
{
#if __cplusplus >= 201703L
#define CONSTEXPR constexpr
#else
#define CONSTEXPR
#endif

// https://en.cppreference.com/w/cpp/iterator/size

// Works with containers
Expand All @@ -72,13 +78,13 @@ TEST_CASE(test_ssize)
// And works with built-in arrays too
int a[]{-5, 10, 15};
// Returns the number of elements (not bytes) as opposed to sizeof
TEST_ASSERT_EQ(std::size(a), 3);
TEST_ASSERT_EQ(CONSTEXPR std::size(a), 3);
static_assert(sizeof(a) == 12, "sizeof(a)");

// Provides a safe way (compared to sizeof) of getting string buffer size
const char str[] = "12345";
// These are fine and give the correct result
TEST_ASSERT_EQ(std::size(str), 6);
TEST_ASSERT_EQ(CONSTEXPR std::size(str), 6);
static_assert(sizeof(str) == 6, "sizeof(str)");

// But use of sizeof here is a common source of bugs
Expand All @@ -89,6 +95,8 @@ TEST_CASE(test_ssize)
auto i = std::ssize(v);
for (--i; i != -1; --i) { }
TEST_ASSERT_EQ(i, -1);

#undef CONSTEXPR
}

TEST_MAIN(
Expand Down

0 comments on commit a9002b3

Please sign in to comment.