From 3dce8aaee76a422fd297bd7ac585f4713dcce080 Mon Sep 17 00:00:00 2001 From: neargye Date: Fri, 22 Mar 2019 15:30:32 +0500 Subject: [PATCH] fix nameof_type --- README.md | 4 +-- example/example.cpp | 2 +- include/nameof.hpp | 5 --- test/test.cpp | 83 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2baaee6..e1b57b0 100644 --- a/README.md +++ b/README.md @@ -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() -> "Color" // Name of type -nameof::nameof_type -> "int" +nameof::nameof_type() -> "int" constexpr auto cx_name = NAMEOF_TYPE(Color::RED); static_assert("Color" == cx_name); diff --git a/example/example.cpp b/example/example.cpp index cfbdfae..92618ab 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -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() << std::endl; // SomeStruct std::cout << NAMEOF_TYPE(othervar.ll) << std::endl; // LL std::cout << NAMEOF_TYPE(SomeClass{}) << std::endl; // SomeClass std::cout << NAMEOF_TYPE(othervar.ll) << std::endl; // Long::LL diff --git a/include/nameof.hpp b/include/nameof.hpp index 1289f5b..19bc645 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -250,11 +250,6 @@ template return detail::nameof_type_impl>(); } -template -[[nodiscard]] constexpr std::string_view nameof_type(T) noexcept { - return nameof_type(); -} - } // namespace nameof // NAMEOF used to obtain the simple (unqualified) string name of variable, function, enum, macro. diff --git a/test/test.cpp b/test/test.cpp index 78a87c6..695223c 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -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() == "SomeStruct"); + REQUIRE(nameof::nameof_type() == "SomeStruct *"); + REQUIRE(nameof::nameof_type() == "SomeStruct &"); + REQUIRE(nameof::nameof_type() == "SomeStruct"); + REQUIRE(nameof::nameof_type() == "SomeStruct *"); + REQUIRE(nameof::nameof_type() == "SomeStruct &"); + REQUIRE(nameof::nameof_type() == "const volatile SomeStruct *"); + + REQUIRE(nameof::nameof_type>() == "SomeClass"); + REQUIRE(nameof::nameof_type volatile *>() == "const volatile SomeClass *"); + + REQUIRE(nameof::nameof_type() == "Long"); + REQUIRE(nameof::nameof_type() == "Long"); + REQUIRE(nameof::nameof_type() == "Long::LL"); + + REQUIRE(nameof::nameof_type() == "Color"); +#elif defined(_MSC_VER) + REQUIRE(nameof::nameof_type() == "SomeStruct"); + REQUIRE(nameof::nameof_type() == "SomeStruct *"); + REQUIRE(nameof::nameof_type() == "SomeStruct &"); + REQUIRE(nameof::nameof_type() == "SomeStruct"); + REQUIRE(nameof::nameof_type() == "SomeStruct *"); + REQUIRE(nameof::nameof_type() == "SomeStruct &"); + REQUIRE(nameof::nameof_type() == "SomeStruct const volatile *"); + + REQUIRE(nameof::nameof_type>() == "SomeClass"); + REQUIRE(nameof::nameof_type volatile *>() == "SomeClass const volatile *"); + + REQUIRE(nameof::nameof_type() == "Long"); + REQUIRE(nameof::nameof_type() == "Long"); + REQUIRE(nameof::nameof_type() == "Long::LL"); + + REQUIRE(nameof::nameof_type() == "Color"); +#elif defined(__GNUC__) + REQUIRE(nameof::nameof_type() == "SomeStruct"); + REQUIRE(nameof::nameof_type() == "SomeStruct*"); + REQUIRE(nameof::nameof_type() == "SomeStruct&"); + REQUIRE(nameof::nameof_type() == "SomeStruct"); + REQUIRE(nameof::nameof_type() == "SomeStruct*"); + REQUIRE(nameof::nameof_type() == "SomeStruct&"); + REQUIRE(nameof::nameof_type() == "const volatile SomeStruct*"); + + REQUIRE(nameof::nameof_type>() == "SomeClass"); + REQUIRE(nameof::nameof_type volatile *>() == "const volatile SomeClass*"); + + REQUIRE(nameof::nameof_type() == "Long"); + REQUIRE(nameof::nameof_type() == "Long"); + REQUIRE(nameof::nameof_type() == "Long::LL"); + + REQUIRE(nameof::nameof_type() == "Color"); +#endif +} + TEST_CASE("Spaces and Tabs ignored") { SECTION("Spaces") { REQUIRE(NAMEOF( struct_var ) == "struct_var");