fix nameof_type

This commit is contained in:
neargye 2019-03-22 15:30:32 +05:00
parent 68ad4fba4e
commit 3dce8aaee7
4 changed files with 86 additions and 8 deletions

View file

@ -72,9 +72,9 @@ NAMEOF_TYPE(Color::RED) -> "Color"
// Name of type
NAMEOF_TYPE_T(int) -> "int"
// Name of variable type
nameof::nameof_type(Color::RED) -> "Color"
nameof::nameof_type<decltype(Color::RED)>() -> "Color"
// Name of type
nameof::nameof_type<int> -> "int"
nameof::nameof_type<int>() -> "int"
constexpr auto cx_name = NAMEOF_TYPE(Color::RED);
static_assert("Color" == cx_name);

View file

@ -116,7 +116,7 @@ int main() {
// Type name.
std::cout << NAMEOF_TYPE(structvar) << std::endl; // SomeStruct
std::cout << nameof::nameof_type(structvar) << std::endl; // SomeStruct
std::cout << nameof::nameof_type<decltype(structvar)>() << std::endl; // SomeStruct
std::cout << NAMEOF_TYPE(othervar.ll) << std::endl; // LL
std::cout << NAMEOF_TYPE(SomeClass<int>{}) << std::endl; // SomeClass
std::cout << NAMEOF_TYPE(othervar.ll) << std::endl; // Long::LL

View file

@ -250,11 +250,6 @@ template <typename T>
return detail::nameof_type_impl<detail::identity<T>>();
}
template <typename T>
[[nodiscard]] constexpr std::string_view nameof_type(T) noexcept {
return nameof_type<T>();
}
} // namespace nameof
// NAMEOF used to obtain the simple (unqualified) string name of variable, function, enum, macro.

View file

@ -237,6 +237,34 @@ TEST_CASE("NAMEOF_ENUM") {
#endif
}
TEST_CASE("nameof::nameof_enum(value)") {
Color color_ = Color::BLUE;
Color m[3] = {Color::RED, Color::GREEN, Color::BLUE};
#if defined(__clang__) || defined(_MSC_VER)
REQUIRE(nameof::nameof_enum(Color::RED) == "RED");
REQUIRE(nameof::nameof_enum(color) == "RED");
REQUIRE(nameof::nameof_enum(Color::BLUE) == "BLUE");
REQUIRE(nameof::nameof_enum(color_) == "BLUE");
REQUIRE(nameof::nameof_enum(m[1]) == "GREEN");
REQUIRE(nameof::nameof_enum(Directions::Right) == "Right");
REQUIRE(nameof::nameof_enum(directions) == "Right");
#elif defined(__GNUC__)
REQUIRE(nameof::nameof_enum(Color::RED) == "(Color)-1");
REQUIRE(nameof::nameof_enum(color) == "(Color)-1");
REQUIRE(nameof::nameof_enum(Color::BLUE) == "(Color)1");
REQUIRE(nameof::nameof_enum(color_) == "(Color)1");
REQUIRE(nameof::nameof_enum(m[1]) == "(Color)0");
REQUIRE(nameof::nameof_enum(Directions::Right) == "(Directions)2");
REQUIRE(nameof::nameof_enum(directions) == "(Directions)2");
#endif
}
TEST_CASE("NAMEOF_TYPE") {
#if defined(__clang__)
REQUIRE(NAMEOF_TYPE(struct_var) == "SomeStruct");
@ -338,6 +366,61 @@ TEST_CASE("NAMEOF_TYPE_T") {
#endif
}
TEST_CASE("nameof::nameof_type()"){
#if defined(__clang__)
REQUIRE(nameof::nameof_type<decltype(struct_var)>() == "SomeStruct");
REQUIRE(nameof::nameof_type<decltype(ptr_s)>() == "SomeStruct *");
REQUIRE(nameof::nameof_type<decltype(ref_s)>() == "SomeStruct &");
REQUIRE(nameof::nameof_type<SomeStruct>() == "SomeStruct");
REQUIRE(nameof::nameof_type<SomeStruct *>() == "SomeStruct *");
REQUIRE(nameof::nameof_type<SomeStruct &>() == "SomeStruct &");
REQUIRE(nameof::nameof_type<const SomeStruct volatile *>() == "const volatile SomeStruct *");
REQUIRE(nameof::nameof_type<SomeClass<int>>() == "SomeClass<int>");
REQUIRE(nameof::nameof_type<const SomeClass<int> volatile *>() == "const volatile SomeClass<int> *");
REQUIRE(nameof::nameof_type<decltype(othervar)>() == "Long");
REQUIRE(nameof::nameof_type<Long>() == "Long");
REQUIRE(nameof::nameof_type<Long::LL>() == "Long::LL");
REQUIRE(nameof::nameof_type<Color>() == "Color");
#elif defined(_MSC_VER)
REQUIRE(nameof::nameof_type<decltype(struct_var)>() == "SomeStruct");
REQUIRE(nameof::nameof_type<decltype(ptr_s)>() == "SomeStruct *");
REQUIRE(nameof::nameof_type<decltype(ref_s)>() == "SomeStruct &");
REQUIRE(nameof::nameof_type<SomeStruct>() == "SomeStruct");
REQUIRE(nameof::nameof_type<SomeStruct *>() == "SomeStruct *");
REQUIRE(nameof::nameof_type<SomeStruct &>() == "SomeStruct &");
REQUIRE(nameof::nameof_type<const SomeStruct volatile *>() == "SomeStruct const volatile *");
REQUIRE(nameof::nameof_type<SomeClass<int>>() == "SomeClass<int>");
REQUIRE(nameof::nameof_type<const SomeClass<int> volatile *>() == "SomeClass<int> const volatile *");
REQUIRE(nameof::nameof_type<decltype(othervar)>() == "Long");
REQUIRE(nameof::nameof_type<Long>() == "Long");
REQUIRE(nameof::nameof_type<Long::LL>() == "Long::LL");
REQUIRE(nameof::nameof_type<Color>() == "Color");
#elif defined(__GNUC__)
REQUIRE(nameof::nameof_type<decltype(struct_var)>() == "SomeStruct");
REQUIRE(nameof::nameof_type<decltype(ptr_s)>() == "SomeStruct*");
REQUIRE(nameof::nameof_type<decltype(ref_s)>() == "SomeStruct&");
REQUIRE(nameof::nameof_type<SomeStruct>() == "SomeStruct");
REQUIRE(nameof::nameof_type<SomeStruct *>() == "SomeStruct*");
REQUIRE(nameof::nameof_type<SomeStruct &>() == "SomeStruct&");
REQUIRE(nameof::nameof_type<const SomeStruct volatile *>() == "const volatile SomeStruct*");
REQUIRE(nameof::nameof_type<SomeClass<int>>() == "SomeClass<int>");
REQUIRE(nameof::nameof_type<const SomeClass<int> volatile *>() == "const volatile SomeClass<int>*");
REQUIRE(nameof::nameof_type<decltype(othervar)>() == "Long");
REQUIRE(nameof::nameof_type<Long>() == "Long");
REQUIRE(nameof::nameof_type<Long::LL>() == "Long::LL");
REQUIRE(nameof::nameof_type<Color>() == "Color");
#endif
}
TEST_CASE("Spaces and Tabs ignored") {
SECTION("Spaces") {
REQUIRE(NAMEOF( struct_var ) == "struct_var");