From 4b301ad8cb1a3a69f25fec755e8f045798617344 Mon Sep 17 00:00:00 2001 From: terik23 Date: Tue, 2 Apr 2019 00:07:28 +0500 Subject: [PATCH] wip v0.8.0 --- example/example.cpp | 45 +++++---- include/nameof.hpp | 86 ++++++++--------- test/test.cpp | 228 ++++++++++++++++++++++++-------------------- 3 files changed, 192 insertions(+), 167 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index cd157b2..49ba539 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -51,7 +51,7 @@ void SomeMethod3() { template std::string SomeMethod4(U value) { std::stringstream s; - s << NAMEOF(SomeMethod4) << "<" << NAMEOF_TYPE_T(T) << ", " << NAMEOF_TYPE_T(U) << ">(" << NAMEOF_TYPE_T(U) << " " << NAMEOF(value) << ")"; + s << NAMEOF(SomeMethod4) << "<" << NAMEOF_TYPE(T) << ", " << NAMEOF_TYPE(U) << ">(" << NAMEOF_TYPE(U) << " " << NAMEOF(value) << ")"; return s.str(); } @@ -65,7 +65,7 @@ public: template C SomeMethod6() const { C t{}; - std::cout << NAMEOF_TYPE(t) << std::endl; + std::cout << NAMEOF_VAR_TYPE(t) << std::endl; return t; } }; @@ -85,15 +85,16 @@ SomeStruct* ptrvar = &structvar; int main() { // Compile-time. - constexpr auto cx_name = NAMEOF(structvar); - static_assert("structvar" == cx_name); + constexpr auto name = NAMEOF(structvar); + static_assert("structvar" == name); // Enum name. auto color = Color::RED; std::cout << NAMEOF_ENUM(color) << std::endl; // RED std::cout << nameof::nameof_enum(color) << std::endl; // RED - constexpr auto cx_color = Color::RED; - std::cout << nameof::nameof_enum() << std::endl; // RED + constexpr auto const_color = Color::BLUE; + std::cout << nameof::nameof_enum() << std::endl; // BLUE + std::cout << NAMEOF_CONST_ENUM(const_color) << std::endl; // BLUE // Variable name. std::cout << NAMEOF(structvar) << std::endl; // structvar @@ -117,23 +118,26 @@ int main() { std::cout << NAMEOF_FULL(&SomeClass::SomeMethod6) << std::endl; // SomeMethod6 // Type name. - std::cout << NAMEOF_TYPE(structvar) << std::endl; // SomeStruct + std::cout << NAMEOF_VAR_TYPE(structvar) << std::endl; // SomeStruct std::cout << nameof::nameof_type() << std::endl; // SomeStruct - std::cout << NAMEOF_TYPE(SomeClass{}) << std::endl; // SomeClass - std::cout << NAMEOF_TYPE(othervar.ll) << std::endl; // Long::LL - std::cout << NAMEOF_TYPE(std::declval>()) << std::endl; // const SomeClass && + std::cout << NAMEOF_VAR_TYPE(SomeClass{}) << std::endl; // SomeClass + std::cout << NAMEOF_VAR_TYPE(othervar.ll) << std::endl; // Long::LL + std::cout << NAMEOF_VAR_TYPE(std::declval>()) << std::endl; // const SomeClass && - std::cout << NAMEOF_TYPE_T(const SomeClass volatile *) << std::endl; // const volatile SomeClass * - std::cout << NAMEOF_TYPE_T(SomeClass) << std::endl; // SomeClass + std::cout << NAMEOF_TYPE(const SomeClass volatile *) << std::endl; // const volatile SomeClass * + std::cout << NAMEOF_TYPE(SomeClass) << std::endl; // SomeClass std::cout << nameof::nameof_type>() << std::endl; // SomeClass + // Macro name. + std::cout << NAMEOF(__LINE__) << std::endl; // __LINE__ + std::cout << NAMEOF(NAMEOF(structvar)) << std::endl; // 'NAMEOF' + // Raw name. - std::cout << NAMEOF_RAW(__LINE__) << std::endl; // __LINE__ std::cout << NAMEOF_RAW(structvar.somefield) << std::endl; // structvar.somefield std::cout << NAMEOF_RAW(&SomeStruct::SomeMethod1) << std::endl; // &SomeStruct::SomeMethod1 std::cout << NAMEOF_RAW(const SomeClass volatile *) << std::endl; // const SomeClass volatile * - // Some more example. + // Some more complex example. std::cout << SomeMethod4(structvar) << std::endl; // SomeMethod4(SomeStruct value) @@ -152,21 +156,20 @@ int main() { } /* Remarks */ -#if 0 +#if 1 // This expression does not have name. std::cout << NAMEOF("Bad case"_string) << std::endl; // '_string' - std::cout << NAMEOF(42.0) << std::endl; // '0' + std::cout << NAMEOF(42.0) << std::endl; // '' std::cout << NAMEOF(42.f) << std::endl; // 'f' - std::cout << NAMEOF(42) << std::endl; // '42' - std::cout << NAMEOF(42.0_deg) << std::endl; // '0_deg' + std::cout << NAMEOF(42) << std::endl; // '' + std::cout << NAMEOF(42.0_deg) << std::endl; // '' std::cout << NAMEOF(std::string()) << std::endl; // 'string' std::cout << NAMEOF(std::string{}) << std::endl; // 'string' std::cout << NAMEOF(std::string{"test"}) << std::endl; // 'string' std::cout << NAMEOF(structvar.somefield + structvar.somefield) << std::endl; // ' somefield' - std::cout << NAMEOF(42 + 42) << std::endl; // ' 42' - std::cout << NAMEOF(NAMEOF(structvar)) << std::endl; // 'NAMEOF' + std::cout << NAMEOF(42 + 42) << std::endl; // '' std::cout << NAMEOF((SomeMethod4)(1.0f)) << std::endl; // '' - std::cout << NAMEOF(42, 42, 42) << std::endl; // '42' + std::cout << NAMEOF(42, 42, 42) << std::endl; // '' #endif #if 0 diff --git a/include/nameof.hpp b/include/nameof.hpp index a57d453..33cd9a8 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -36,18 +36,18 @@ #include #include -#if !defined(NAMEOF_ENUM_MAX_SEARCH_DEPTH) -# define NAMEOF_ENUM_MAX_SEARCH_DEPTH 128 +#if !defined(NAMEOF_ENUM_RANGE) +# define NAMEOF_ENUM_RANGE 128 #endif namespace nameof { -static_assert(NAMEOF_ENUM_MAX_SEARCH_DEPTH > 0, - "NAMEOF_ENUM_MAX_SEARCH_DEPTH must be positive and greater than zero."); -static_assert(NAMEOF_ENUM_MAX_SEARCH_DEPTH % 8 == 0, - "NAMEOF_ENUM_MAX_SEARCH_DEPTH must be a multiple of 8."); -static_assert(NAMEOF_ENUM_MAX_SEARCH_DEPTH < std::numeric_limits::max(), - "NAMEOF_ENUM_MAX_SEARCH_DEPTH must be less INT_MAX."); +static_assert(NAMEOF_ENUM_RANGE > 0, + "NAMEOF_ENUM_RANGE must be positive and greater than zero."); +static_assert(NAMEOF_ENUM_RANGE % 8 == 0, + "NAMEOF_ENUM_RANGE must be a multiple of 8."); +static_assert(NAMEOF_ENUM_RANGE < std::numeric_limits::max(), + "NAMEOF_ENUM_RANGE must be less INT_MAX."); namespace detail { @@ -56,17 +56,17 @@ struct identity final { using type = T; }; -[[nodiscard]] constexpr bool is_name_char(char c) noexcept { - return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'; +[[nodiscard]] constexpr bool is_name_char(char c, bool front) noexcept { + return (!front && c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'; } [[nodiscard]] constexpr bool is_bracket_char(char c) noexcept { return c == ')' || c == '}' || c == '>' || c == '(' || c == '{' || c == '<'; } -[[nodiscard]] constexpr std::string_view pretty_name(std::string_view name, bool with_suffix) noexcept { +[[nodiscard]] constexpr std::string_view pretty_name(std::string_view name, bool with_template_suffix) noexcept { for (std::size_t i = name.size(), h = 0, s = 0; i > 0; --i) { - if (h == 0 && !is_name_char(name[i - 1]) && !is_bracket_char(name[i - 1])) { + if (h == 0 && !is_name_char(name[i - 1], false) && !is_bracket_char(name[i - 1])) { ++s; continue; } @@ -111,14 +111,18 @@ struct identity final { } for (std::size_t i = name.size() - s; i > 0; --i) { - if (!is_name_char(name[i - 1])) { + if (!is_name_char(name[i - 1], false)) { name.remove_prefix(i); break; } } - name.remove_suffix(with_suffix ? 0 : s); + name.remove_suffix(with_template_suffix ? 0 : s); - return name; + if (name.length() > 0 && is_name_char(name.front(), true)) { + return name; + } else { + return {}; // Does not have name. + } } template @@ -156,14 +160,13 @@ template while (name.back() == ' ') { name.remove_suffix(1); } - return name; #endif } template [[nodiscard]] constexpr std::string_view nameof_enum_impl() noexcept { - static_assert(std::is_enum_v); + static_assert(std::is_enum_v, "nameof::nameof_enum require enum type."); #if defined(__clang__) std::string_view name{__PRETTY_FUNCTION__}; constexpr auto suffix = sizeof("]") - 1; @@ -179,25 +182,14 @@ template #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER) name.remove_suffix(suffix); - for (std::size_t i = name.size(); i > 0; --i) { - if (!is_name_char(name[i - 1])) { - name.remove_prefix(i); - break; - } - } - - if (name.front() >= '0' && name.front() <= '9') { - return {}; // Enum variable does not have name. - } else { - return name; - } + return pretty_name(name, false); #endif } template struct nameof_enum_impl_t final { [[nodiscard]] constexpr std::string_view operator()(int value) const noexcept { - static_assert(std::is_enum_v); + static_assert(std::is_enum_v, "nameof::nameof_enum require enum type."); if constexpr (V > std::numeric_limits>::max()) { return {}; // Enum variable out of range. } @@ -226,16 +218,16 @@ struct nameof_enum_impl_t final { }; template -struct nameof_enum_impl_t final { +struct nameof_enum_impl_t final { [[nodiscard]] constexpr std::string_view operator()(int) const noexcept { - static_assert(std::is_enum_v); - return {}; // Enum variable out of range NAMEOF_ENUM_MAX_SEARCH_DEPTH. + static_assert(std::is_enum_v, "nameof::nameof_enum require enum type."); + return {}; // Enum variable out of range NAMEOF_ENUM_RANGE. } }; template >> -[[nodiscard]] constexpr std::string_view nameof_impl(std::string_view name, bool with_suffix) noexcept { - return pretty_name(name, with_suffix); +[[nodiscard]] constexpr std::string_view nameof_impl(std::string_view name, bool with_template_suffix) noexcept { + return pretty_name(name, with_template_suffix); } [[nodiscard]] constexpr std::string_view nameof_raw_impl(std::string_view name) noexcept { @@ -244,21 +236,24 @@ template >> } // namespace detail -// nameof_enum used to obtain the simple (unqualified) string enum name of enum variable. +// nameof_enum(enum) used to obtain the simple (unqualified) string enum name of enum variable. template >>> [[nodiscard]] constexpr std::string_view nameof_enum(T value) noexcept { constexpr bool s = std::is_signed_v>>; - constexpr int min = s ? -NAMEOF_ENUM_MAX_SEARCH_DEPTH : 0; + constexpr int min = s ? -NAMEOF_ENUM_RANGE : 0; + if (static_cast(value) >= NAMEOF_ENUM_RANGE || static_cast(value) <= -NAMEOF_ENUM_RANGE) { + return {}; // Enum variable out of range NAMEOF_ENUM_RANGE. + } return detail::nameof_enum_impl_t, min>{}(static_cast(value)); } -// nameof_enum used to obtain the simple (unqualified) string enum name of static storage enum variable. +// nameof_enum() used to obtain the simple (unqualified) string enum name of static storage enum variable. template >>> [[nodiscard]] constexpr std::string_view nameof_enum() noexcept { return detail::nameof_enum_impl(); } -// nameof_type used to obtain the string name of type. +// nameof_type() used to obtain the string name of type. template [[nodiscard]] constexpr std::string_view nameof_type() noexcept { return detail::nameof_type_impl>(); @@ -269,17 +264,20 @@ template // NAMEOF used to obtain the simple (unqualified) string name of variable, function, enum, macro. #define NAMEOF(...) ::nameof::detail::nameof_impl(#__VA_ARGS__, false) -// NAMEOF_FULL used to obtain the full string name of variable, function, enum, macro. +// NAMEOF_FULL used to obtain the simple (unqualified) full (with template suffix) string name of variable, function, enum, macro. #define NAMEOF_FULL(...) ::nameof::detail::nameof_impl(#__VA_ARGS__, true) // NAMEOF_ENUM used to obtain the simple (unqualified) string enum name of enum variable. #define NAMEOF_ENUM(...) ::nameof::nameof_enum(__VA_ARGS__) -// NAMEOF_TYPE used to obtain the string name of variable type. -#define NAMEOF_TYPE(...) ::nameof::nameof_type() +// NAMEOF_CONST_ENUM used to obtain the simple (unqualified) string enum name of static storage enum variable. +#define NAMEOF_CONST_ENUM(...) ::nameof::nameof_enum<__VA_ARGS__>() -// NAMEOF_TYPE_T used to obtain the string name of type. -#define NAMEOF_TYPE_T(...) ::nameof::nameof_type<__VA_ARGS__>() +// NAMEOF_TYPE used to obtain the string name of type. +#define NAMEOF_TYPE(...) ::nameof::nameof_type<__VA_ARGS__>() + +// NAMEOF_VAR_TYPE used to obtain the string name of variable type. +#define NAMEOF_VAR_TYPE(...) ::nameof::nameof_type() // NAMEOF_RAW used to obtain the raw string name of variable, function, enum, macro. #define NAMEOF_RAW(...) ::nameof::detail::nameof_raw_impl(#__VA_ARGS__) diff --git a/test/test.cpp b/test/test.cpp index 0c1b6b4..b5ebcd1 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -23,7 +23,7 @@ #define CATCH_CONFIG_MAIN #include -#define NAMEOF_ENUM_MAX_SEARCH_DEPTH 120 +#define NAMEOF_ENUM_RANGE 120 #include #include @@ -50,13 +50,13 @@ template class SomeClass { public: void SomeMethod5() const { - std::cout << NAMEOF_TYPE_T(T) << std::endl; + std::cout << NAMEOF_TYPE(T) << std::endl; } template C SomeMethod6() const { C t{}; - std::cout << NAMEOF_TYPE(t) << std::endl; + std::cout << NAMEOF_VAR_TYPE(t) << std::endl; return t; } }; @@ -225,16 +225,16 @@ TEST_CASE("NAMEOF_ENUM") { REQUIRE(NAMEOF_ENUM(Directions::Right) == "Right"); REQUIRE(NAMEOF_ENUM(directions) == "Right"); - REQUIRE(NAMEOF_ENUM((Color)NAMEOF_ENUM_MAX_SEARCH_DEPTH).empty()); + REQUIRE(NAMEOF_ENUM((Color)NAMEOF_ENUM_RANGE).empty()); REQUIRE(NAMEOF_ENUM((Color)-100).empty()); REQUIRE(NAMEOF_ENUM((Color)100).empty()); - REQUIRE(NAMEOF_ENUM((Directions)NAMEOF_ENUM_MAX_SEARCH_DEPTH).empty()); + REQUIRE(NAMEOF_ENUM((Directions)NAMEOF_ENUM_RANGE).empty()); REQUIRE(NAMEOF_ENUM((Directions)100).empty()); REQUIRE(NAMEOF_ENUM((Directions)100).empty()); #endif } -TEST_CASE("nameof::nameof_enum(value)") { +TEST_CASE("nameof::nameof_enum(enum)") { #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER) Color color_ = Color::BLUE; Color m[3] = {Color::RED, Color::GREEN, Color::BLUE}; @@ -249,18 +249,42 @@ TEST_CASE("nameof::nameof_enum(value)") { REQUIRE(nameof::nameof_enum(Directions::Right) == "Right"); REQUIRE(nameof::nameof_enum(directions) == "Right"); - REQUIRE(nameof::nameof_enum((Color)NAMEOF_ENUM_MAX_SEARCH_DEPTH).empty()); + REQUIRE(nameof::nameof_enum((Color)NAMEOF_ENUM_RANGE).empty()); REQUIRE(nameof::nameof_enum((Color)-100).empty()); REQUIRE(nameof::nameof_enum((Color)100).empty()); - REQUIRE(nameof::nameof_enum((Directions)NAMEOF_ENUM_MAX_SEARCH_DEPTH).empty()); + REQUIRE(nameof::nameof_enum((Directions)NAMEOF_ENUM_RANGE).empty()); REQUIRE(nameof::nameof_enum((Directions)100).empty()); REQUIRE(nameof::nameof_enum((Directions)100).empty()); #endif } -TEST_CASE("nameof::nameof_enum()") { +TEST_CASE("NAMEOF_CONST_ENUM") { #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER) - constexpr Color color_ = Color::BLUE; + static const Color color_ = Color::BLUE; + constexpr Color m[3] = {Color::RED, Color::GREEN, Color::BLUE}; + REQUIRE(NAMEOF_CONST_ENUM(Color::RED) == "RED"); + REQUIRE(NAMEOF_CONST_ENUM(color) == "RED"); + + REQUIRE(NAMEOF_CONST_ENUM(Color::BLUE) == "BLUE"); + REQUIRE(NAMEOF_CONST_ENUM(color_) == "BLUE"); + + REQUIRE(NAMEOF_CONST_ENUM(m[1]) == "GREEN"); + + REQUIRE(NAMEOF_CONST_ENUM(Directions::Right) == "Right"); + REQUIRE(NAMEOF_CONST_ENUM(directions) == "Right"); + + REQUIRE(NAMEOF_CONST_ENUM((Color)NAMEOF_ENUM_RANGE).empty()); + REQUIRE(NAMEOF_CONST_ENUM((Color)-100).empty()); + REQUIRE(NAMEOF_CONST_ENUM((Color)100).empty()); + REQUIRE(NAMEOF_CONST_ENUM((Directions)NAMEOF_ENUM_RANGE).empty()); + REQUIRE(NAMEOF_CONST_ENUM((Directions)100).empty()); + REQUIRE(NAMEOF_CONST_ENUM((Directions)100).empty()); +#endif +} + +TEST_CASE("nameof::nameof_enum()") { +#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER) + static const Color color_ = Color::BLUE; constexpr Color m[3] = {Color::RED, Color::GREEN, Color::BLUE}; REQUIRE(nameof::nameof_enum() == "RED"); REQUIRE(nameof::nameof_enum() == "RED"); @@ -273,117 +297,117 @@ TEST_CASE("nameof::nameof_enum()") { REQUIRE(nameof::nameof_enum() == "Right"); REQUIRE(nameof::nameof_enum() == "Right"); - REQUIRE(nameof::nameof_enum<(Color)NAMEOF_ENUM_MAX_SEARCH_DEPTH>().empty()); + REQUIRE(nameof::nameof_enum<(Color)NAMEOF_ENUM_RANGE>().empty()); REQUIRE(nameof::nameof_enum<(Color)-100>().empty()); REQUIRE(nameof::nameof_enum<(Color)100>().empty()); - REQUIRE(nameof::nameof_enum<(Directions)NAMEOF_ENUM_MAX_SEARCH_DEPTH>().empty()); + REQUIRE(nameof::nameof_enum<(Directions)NAMEOF_ENUM_RANGE>().empty()); REQUIRE(nameof::nameof_enum<(Directions)100>().empty()); REQUIRE(nameof::nameof_enum<(Directions)100>().empty()); #endif } +TEST_CASE("NAMEOF_VAR_TYPE") { +#if defined(__clang__) + REQUIRE(NAMEOF_VAR_TYPE(struct_var) == "SomeStruct"); + REQUIRE(NAMEOF_VAR_TYPE(ptr_s) == "SomeStruct *"); + REQUIRE(NAMEOF_VAR_TYPE(ref_s) == "SomeStruct &"); + + REQUIRE(NAMEOF_VAR_TYPE(ptr_c) == "const volatile SomeClass *"); + + REQUIRE(NAMEOF_VAR_TYPE(othervar) == "Long"); + REQUIRE(NAMEOF_VAR_TYPE(othervar.ll) == "Long::LL"); + REQUIRE(NAMEOF_VAR_TYPE(othervar.ll.field) == "int"); + + REQUIRE(NAMEOF_VAR_TYPE(Color::RED) == "Color"); + + REQUIRE(NAMEOF_VAR_TYPE(std::declval>()) == "const SomeClass &&"); +#elif defined(_MSC_VER) + REQUIRE(NAMEOF_VAR_TYPE(struct_var) == "SomeStruct"); + REQUIRE(NAMEOF_VAR_TYPE(ptr_s) == "SomeStruct *"); + REQUIRE(NAMEOF_VAR_TYPE(ref_s) == "SomeStruct &"); + + REQUIRE(NAMEOF_VAR_TYPE(ptr_c) == "SomeClass const volatile *"); + + REQUIRE(NAMEOF_VAR_TYPE(othervar) == "Long"); + REQUIRE(NAMEOF_VAR_TYPE(othervar.ll) == "Long::LL"); + REQUIRE(NAMEOF_VAR_TYPE(othervar.ll.field) == "int"); + + REQUIRE(NAMEOF_VAR_TYPE(Color::RED) == "Color"); + + REQUIRE(NAMEOF_VAR_TYPE(std::declval>()) == "SomeClass const &&"); +#elif defined(__GNUC__) + REQUIRE(NAMEOF_VAR_TYPE(struct_var) == "SomeStruct"); + REQUIRE(NAMEOF_VAR_TYPE(ptr_s) == "SomeStruct*"); + REQUIRE(NAMEOF_VAR_TYPE(ref_s) == "SomeStruct&"); + + REQUIRE(NAMEOF_VAR_TYPE(ptr_c) == "const volatile SomeClass*"); + + REQUIRE(NAMEOF_VAR_TYPE(othervar) == "Long"); + REQUIRE(NAMEOF_VAR_TYPE(othervar.ll) == "Long::LL"); + REQUIRE(NAMEOF_VAR_TYPE(othervar.ll.field) == "int"); + + REQUIRE(NAMEOF_VAR_TYPE(Color::RED) == "Color"); + + REQUIRE(NAMEOF_VAR_TYPE(std::declval>()) == "const SomeClass&&"); +#endif +} + TEST_CASE("NAMEOF_TYPE") { #if defined(__clang__) - REQUIRE(NAMEOF_TYPE(struct_var) == "SomeStruct"); - REQUIRE(NAMEOF_TYPE(ptr_s) == "SomeStruct *"); - REQUIRE(NAMEOF_TYPE(ref_s) == "SomeStruct &"); + REQUIRE(NAMEOF_TYPE(decltype(struct_var)) == "SomeStruct"); + REQUIRE(NAMEOF_TYPE(decltype(ptr_s)) == "SomeStruct *"); + REQUIRE(NAMEOF_TYPE(decltype(ref_s)) == "SomeStruct &"); + REQUIRE(NAMEOF_TYPE(SomeStruct) == "SomeStruct"); + REQUIRE(NAMEOF_TYPE(SomeStruct *) == "SomeStruct *"); + REQUIRE(NAMEOF_TYPE(SomeStruct &) == "SomeStruct &"); + REQUIRE(NAMEOF_TYPE(const SomeStruct volatile *) == "const volatile SomeStruct *"); - REQUIRE(NAMEOF_TYPE(ptr_c) == "const volatile SomeClass *"); + REQUIRE(NAMEOF_TYPE(SomeClass) == "SomeClass"); + REQUIRE(NAMEOF_TYPE(const SomeClass volatile *) == "const volatile SomeClass *"); - REQUIRE(NAMEOF_TYPE(othervar) == "Long"); - REQUIRE(NAMEOF_TYPE(othervar.ll) == "Long::LL"); - REQUIRE(NAMEOF_TYPE(othervar.ll.field) == "int"); + REQUIRE(NAMEOF_TYPE(decltype(othervar)) == "Long"); + REQUIRE(NAMEOF_TYPE(Long) == "Long"); + REQUIRE(NAMEOF_TYPE(Long::LL) == "Long::LL"); - REQUIRE(NAMEOF_TYPE(Color::RED) == "Color"); - - REQUIRE(NAMEOF_TYPE(std::declval>()) == "const SomeClass &&"); + REQUIRE(NAMEOF_TYPE(Color) == "Color"); #elif defined(_MSC_VER) - REQUIRE(NAMEOF_TYPE(struct_var) == "SomeStruct"); - REQUIRE(NAMEOF_TYPE(ptr_s) == "SomeStruct *"); - REQUIRE(NAMEOF_TYPE(ref_s) == "SomeStruct &"); + REQUIRE(NAMEOF_TYPE(decltype(struct_var)) == "SomeStruct"); + REQUIRE(NAMEOF_TYPE(decltype(ptr_s)) == "SomeStruct *"); + REQUIRE(NAMEOF_TYPE(decltype(ref_s)) == "SomeStruct &"); + REQUIRE(NAMEOF_TYPE(SomeStruct) == "SomeStruct"); + REQUIRE(NAMEOF_TYPE(SomeStruct *) == "SomeStruct *"); + REQUIRE(NAMEOF_TYPE(SomeStruct &) == "SomeStruct &"); + REQUIRE(NAMEOF_TYPE(const SomeStruct volatile *) == "SomeStruct const volatile *"); - REQUIRE(NAMEOF_TYPE(ptr_c) == "SomeClass const volatile *"); + REQUIRE(NAMEOF_TYPE(SomeClass) == "SomeClass"); + REQUIRE(NAMEOF_TYPE(const SomeClass volatile *) == "SomeClass const volatile *"); - REQUIRE(NAMEOF_TYPE(othervar) == "Long"); - REQUIRE(NAMEOF_TYPE(othervar.ll) == "Long::LL"); - REQUIRE(NAMEOF_TYPE(othervar.ll.field) == "int"); + REQUIRE(NAMEOF_TYPE(decltype(othervar)) == "Long"); + REQUIRE(NAMEOF_TYPE(Long) == "Long"); + REQUIRE(NAMEOF_TYPE(Long::LL) == "Long::LL"); - REQUIRE(NAMEOF_TYPE(Color::RED) == "Color"); - - REQUIRE(NAMEOF_TYPE(std::declval>()) == "SomeClass const &&"); + REQUIRE(NAMEOF_TYPE(Color) == "Color"); #elif defined(__GNUC__) - REQUIRE(NAMEOF_TYPE(struct_var) == "SomeStruct"); - REQUIRE(NAMEOF_TYPE(ptr_s) == "SomeStruct*"); - REQUIRE(NAMEOF_TYPE(ref_s) == "SomeStruct&"); + REQUIRE(NAMEOF_TYPE(decltype(struct_var)) == "SomeStruct"); + REQUIRE(NAMEOF_TYPE(decltype(ptr_s)) == "SomeStruct*"); + REQUIRE(NAMEOF_TYPE(decltype(ref_s)) == "SomeStruct&"); + REQUIRE(NAMEOF_TYPE(SomeStruct) == "SomeStruct"); + REQUIRE(NAMEOF_TYPE(SomeStruct *) == "SomeStruct*"); + REQUIRE(NAMEOF_TYPE(SomeStruct &) == "SomeStruct&"); + REQUIRE(NAMEOF_TYPE(const SomeStruct volatile *) == "const volatile SomeStruct*"); - REQUIRE(NAMEOF_TYPE(ptr_c) == "const volatile SomeClass*"); + REQUIRE(NAMEOF_TYPE(SomeClass) == "SomeClass"); + REQUIRE(NAMEOF_TYPE(const SomeClass volatile *) == "const volatile SomeClass*"); - REQUIRE(NAMEOF_TYPE(othervar) == "Long"); - REQUIRE(NAMEOF_TYPE(othervar.ll) == "Long::LL"); - REQUIRE(NAMEOF_TYPE(othervar.ll.field) == "int"); + REQUIRE(NAMEOF_TYPE(decltype(othervar)) == "Long"); + REQUIRE(NAMEOF_TYPE(Long) == "Long"); + REQUIRE(NAMEOF_TYPE(Long::LL) == "Long::LL"); - REQUIRE(NAMEOF_TYPE(Color::RED) == "Color"); - - REQUIRE(NAMEOF_TYPE(std::declval>()) == "const SomeClass&&"); + REQUIRE(NAMEOF_TYPE(Color) == "Color"); #endif } -TEST_CASE("NAMEOF_TYPE_T") { -#if defined(__clang__) - REQUIRE(NAMEOF_TYPE_T(decltype(struct_var)) == "SomeStruct"); - REQUIRE(NAMEOF_TYPE_T(decltype(ptr_s)) == "SomeStruct *"); - REQUIRE(NAMEOF_TYPE_T(decltype(ref_s)) == "SomeStruct &"); - REQUIRE(NAMEOF_TYPE_T(SomeStruct) == "SomeStruct"); - REQUIRE(NAMEOF_TYPE_T(SomeStruct *) == "SomeStruct *"); - REQUIRE(NAMEOF_TYPE_T(SomeStruct &) == "SomeStruct &"); - REQUIRE(NAMEOF_TYPE_T(const SomeStruct volatile *) == "const volatile SomeStruct *"); - - REQUIRE(NAMEOF_TYPE_T(SomeClass) == "SomeClass"); - REQUIRE(NAMEOF_TYPE_T(const SomeClass volatile *) == "const volatile SomeClass *"); - - REQUIRE(NAMEOF_TYPE_T(decltype(othervar)) == "Long"); - REQUIRE(NAMEOF_TYPE_T(Long) == "Long"); - REQUIRE(NAMEOF_TYPE_T(Long::LL) == "Long::LL"); - - REQUIRE(NAMEOF_TYPE_T(Color) == "Color"); -#elif defined(_MSC_VER) - REQUIRE(NAMEOF_TYPE_T(decltype(struct_var)) == "SomeStruct"); - REQUIRE(NAMEOF_TYPE_T(decltype(ptr_s)) == "SomeStruct *"); - REQUIRE(NAMEOF_TYPE_T(decltype(ref_s)) == "SomeStruct &"); - REQUIRE(NAMEOF_TYPE_T(SomeStruct) == "SomeStruct"); - REQUIRE(NAMEOF_TYPE_T(SomeStruct *) == "SomeStruct *"); - REQUIRE(NAMEOF_TYPE_T(SomeStruct &) == "SomeStruct &"); - REQUIRE(NAMEOF_TYPE_T(const SomeStruct volatile *) == "SomeStruct const volatile *"); - - REQUIRE(NAMEOF_TYPE_T(SomeClass) == "SomeClass"); - REQUIRE(NAMEOF_TYPE_T(const SomeClass volatile *) == "SomeClass const volatile *"); - - REQUIRE(NAMEOF_TYPE_T(decltype(othervar)) == "Long"); - REQUIRE(NAMEOF_TYPE_T(Long) == "Long"); - REQUIRE(NAMEOF_TYPE_T(Long::LL) == "Long::LL"); - - REQUIRE(NAMEOF_TYPE_T(Color) == "Color"); -#elif defined(__GNUC__) - REQUIRE(NAMEOF_TYPE_T(decltype(struct_var)) == "SomeStruct"); - REQUIRE(NAMEOF_TYPE_T(decltype(ptr_s)) == "SomeStruct*"); - REQUIRE(NAMEOF_TYPE_T(decltype(ref_s)) == "SomeStruct&"); - REQUIRE(NAMEOF_TYPE_T(SomeStruct) == "SomeStruct"); - REQUIRE(NAMEOF_TYPE_T(SomeStruct *) == "SomeStruct*"); - REQUIRE(NAMEOF_TYPE_T(SomeStruct &) == "SomeStruct&"); - REQUIRE(NAMEOF_TYPE_T(const SomeStruct volatile *) == "const volatile SomeStruct*"); - - REQUIRE(NAMEOF_TYPE_T(SomeClass) == "SomeClass"); - REQUIRE(NAMEOF_TYPE_T(const SomeClass volatile *) == "const volatile SomeClass*"); - - REQUIRE(NAMEOF_TYPE_T(decltype(othervar)) == "Long"); - REQUIRE(NAMEOF_TYPE_T(Long) == "Long"); - REQUIRE(NAMEOF_TYPE_T(Long::LL) == "Long::LL"); - - REQUIRE(NAMEOF_TYPE_T(Color) == "Color"); -#endif -} - -TEST_CASE("nameof::nameof_type()"){ +TEST_CASE("nameof::nameof_type()"){ #if defined(__clang__) REQUIRE(nameof::nameof_type() == "SomeStruct"); REQUIRE(nameof::nameof_type() == "SomeStruct *"); @@ -446,8 +470,8 @@ TEST_CASE("Spaces and Tabs ignored") { #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER) REQUIRE(NAMEOF_ENUM( color ) == "RED"); #endif - REQUIRE(NAMEOF_TYPE( struct_var ) == "SomeStruct"); - REQUIRE(NAMEOF_TYPE_T( decltype(struct_var) ) == "SomeStruct"); + REQUIRE(NAMEOF_VAR_TYPE( struct_var ) == "SomeStruct"); + REQUIRE(NAMEOF_TYPE( decltype(struct_var) ) == "SomeStruct"); } SECTION("Tabs") { @@ -457,7 +481,7 @@ TEST_CASE("Spaces and Tabs ignored") { #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER) REQUIRE(NAMEOF_ENUM( color ) == "RED"); #endif - REQUIRE(NAMEOF_TYPE( struct_var ) == "SomeStruct"); - REQUIRE(NAMEOF_TYPE_T( decltype(struct_var) ) == "SomeStruct"); + REQUIRE(NAMEOF_VAR_TYPE( struct_var ) == "SomeStruct"); + REQUIRE(NAMEOF_TYPE( decltype(struct_var) ) == "SomeStruct"); } }