Skip to content

Commit a391671

Browse files
committed
add StringFrom for std::nullptr_t values
In C++11, the nullptr_t type was introduced, and in some projects NULL is defined as nullptr. In this case, the C++ compiler will produce warnings when trying to convert NULL to a string, because it is ambiguous which conversion is used. test.cpp:89:2: error: call to 'StringFrom' is ambiguous CHECK_EQUAL(NULL, actual); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/local/include/CppUTest/UtestMacros.h:127:3: note: expanded from macro 'CHECK_EQUAL' CHECK_EQUAL_LOCATION(expected, actual, NULL, __FILE__, __LINE__) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/local/include/CppUTest/UtestMacros.h:138:52: note: expanded from macro 'CHECK_EQUAL_LOCATION' UtestShell::getCurrent()->assertEquals(true, StringFrom(expected).asCharString(), StringFrom(actual).asCharString(), text, file, line); \ ^~~~~~~~~~ /usr/local/include/CppUTest/SimpleString.h:138:14: note: candidate function SimpleString StringFrom(const void* value); ^ /usr/local/include/CppUTest/SimpleString.h:139:14: note: candidate function SimpleString StringFrom(void (*value)()); ^ /usr/local/include/CppUTest/SimpleString.h:141:14: note: candidate function SimpleString StringFrom(const char *value); ^ /usr/local/include/CppUTest/SimpleString.h:137:14: note: candidate function SimpleString StringFrom(bool value); ^ /usr/local/include/CppUTest/SimpleString.h:157:14: note: candidate function SimpleString StringFrom(const SimpleString& other); ^ /usr/local/include/CppUTest/SimpleString.h:171:14: note: candidate function SimpleString StringFrom(const std::string& other); These warnings can be resolved by providing a StringFrom which takes the nullptr_t as the argument, avoiding the need for conversions at all. According to the C++ standard, the define __cplusplus is set to a value greater than 199711L when C++ 11 is supported. Thus, we'll check this when implementing the function, in order to allow building on older C++ standards. Additionally, avoid a compiler warning due to the expected unused parameter in the implementation of StringFrom. Signed-off-by: Jacob Keller <jacob.e.keller@intel.com>
1 parent 1317b74 commit a391671

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

‎include/CppUTest/SimpleString.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ SimpleString BracketsFormattedHexStringFrom(cpputest_ulonglong value);
174174
SimpleString BracketsFormattedHexStringFrom(signed char value);
175175
SimpleString BracketsFormattedHexString(SimpleString hexString);
176176

177-
177+
#if __cplusplus > 199711L
178+
SimpleString StringFrom(const std::nullptr_t value);
179+
#endif
178180

179181
#if CPPUTEST_USE_STD_CPP_LIB
180182

‎src/CppUTest/SimpleString.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,12 @@ SimpleString BracketsFormattedHexString(SimpleString hexString)
540540
return SimpleString("(0x") + hexString + ")" ;
541541
}
542542

543+
#if __cplusplus > 199711L
544+
SimpleString StringFrom(const std::nullptr_t __attribute__((unused)) value)
545+
{
546+
return "(null)";
547+
}
548+
#endif
543549

544550
#ifdef CPPUTEST_USE_LONG_LONG
545551

‎tests/CppUTest/SimpleStringTest.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,16 @@ TEST(SimpleString, Sizes)
572572
STRCMP_EQUAL("10", StringFrom((int) size).asCharString());
573573
}
574574

575+
#if __cplusplus > 199711L
576+
TEST(SimpleString, nullptr_type)
577+
{
578+
SimpleString s(StringFrom(nullptr));
579+
STRCMP_EQUAL("(null)", s.asCharString());
580+
581+
CHECK_EQUAL(nullptr, (nullptr_t)0);
582+
}
583+
#endif
584+
575585
TEST(SimpleString, HexStrings)
576586
{
577587
STRCMP_EQUAL("f3", HexStringFrom((signed char)-13).asCharString());

0 commit comments

Comments
 (0)