wip v0.8.0

This commit is contained in:
terik23 2019-04-02 00:07:28 +05:00
parent de5b02c021
commit 4b301ad8cb
3 changed files with 192 additions and 167 deletions

View file

@ -51,7 +51,7 @@ void SomeMethod3() {
template <typename T, typename U>
std::string SomeMethod4(U value) {
std::stringstream s;
s << NAMEOF(SomeMethod4<T, U>) << "<" << NAMEOF_TYPE_T(T) << ", " << NAMEOF_TYPE_T(U) << ">(" << NAMEOF_TYPE_T(U) << " " << NAMEOF(value) << ")";
s << NAMEOF(SomeMethod4<T, U>) << "<" << NAMEOF_TYPE(T) << ", " << NAMEOF_TYPE(U) << ">(" << NAMEOF_TYPE(U) << " " << NAMEOF(value) << ")";
return s.str();
}
@ -65,7 +65,7 @@ public:
template <typename C>
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<cx_color>() << std::endl; // RED
constexpr auto const_color = Color::BLUE;
std::cout << nameof::nameof_enum<const_color>() << 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<int>::SomeMethod6<long int>) << std::endl; // SomeMethod6<long int>
// Type name.
std::cout << NAMEOF_TYPE(structvar) << std::endl; // SomeStruct
std::cout << NAMEOF_VAR_TYPE(structvar) << std::endl; // SomeStruct
std::cout << nameof::nameof_type<decltype(structvar)>() << std::endl; // SomeStruct
std::cout << NAMEOF_TYPE(SomeClass<int>{}) << std::endl; // SomeClass
std::cout << NAMEOF_TYPE(othervar.ll) << std::endl; // Long::LL
std::cout << NAMEOF_TYPE(std::declval<const SomeClass<int>>()) << std::endl; // const SomeClass<int> &&
std::cout << NAMEOF_VAR_TYPE(SomeClass<int>{}) << std::endl; // SomeClass
std::cout << NAMEOF_VAR_TYPE(othervar.ll) << std::endl; // Long::LL
std::cout << NAMEOF_VAR_TYPE(std::declval<const SomeClass<int>>()) << std::endl; // const SomeClass<int> &&
std::cout << NAMEOF_TYPE_T(const SomeClass<int> volatile *) << std::endl; // const volatile SomeClass<int> *
std::cout << NAMEOF_TYPE_T(SomeClass<int>) << std::endl; // SomeClass<int>
std::cout << NAMEOF_TYPE(const SomeClass<int> volatile *) << std::endl; // const volatile SomeClass<int> *
std::cout << NAMEOF_TYPE(SomeClass<int>) << std::endl; // SomeClass<int>
std::cout << nameof::nameof_type<SomeClass<int>>() << std::endl; // SomeClass<int>
// 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<int> volatile *) << std::endl; // const SomeClass<int> volatile *
// Some more example.
// Some more complex example.
std::cout << SomeMethod4<int>(structvar) << std::endl; // SomeMethod4<int, SomeStruct>(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<int, float>)(1.0f)) << std::endl; // ''
std::cout << NAMEOF(42, 42, 42) << std::endl; // '42'
std::cout << NAMEOF(42, 42, 42) << std::endl; // ''
#endif
#if 0

View file

@ -36,18 +36,18 @@
#include <limits>
#include <string_view>
#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<int>::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<int>::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);
if (name.length() > 0 && is_name_char(name.front(), true)) {
return name;
} else {
return {}; // Does not have name.
}
}
template <typename T>
@ -156,14 +160,13 @@ template <typename T>
while (name.back() == ' ') {
name.remove_suffix(1);
}
return name;
#endif
}
template <auto V>
[[nodiscard]] constexpr std::string_view nameof_enum_impl() noexcept {
static_assert(std::is_enum_v<decltype(V)>);
static_assert(std::is_enum_v<decltype(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 <auto V>
#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 <typename E, int V>
struct nameof_enum_impl_t final {
[[nodiscard]] constexpr std::string_view operator()(int value) const noexcept {
static_assert(std::is_enum_v<E>);
static_assert(std::is_enum_v<E>, "nameof::nameof_enum require enum type.");
if constexpr (V > std::numeric_limits<std::underlying_type_t<E>>::max()) {
return {}; // Enum variable out of range.
}
@ -226,16 +218,16 @@ struct nameof_enum_impl_t final {
};
template <typename E>
struct nameof_enum_impl_t<E, NAMEOF_ENUM_MAX_SEARCH_DEPTH> final {
struct nameof_enum_impl_t<E, NAMEOF_ENUM_RANGE> final {
[[nodiscard]] constexpr std::string_view operator()(int) const noexcept {
static_assert(std::is_enum_v<E>);
return {}; // Enum variable out of range NAMEOF_ENUM_MAX_SEARCH_DEPTH.
static_assert(std::is_enum_v<E>, "nameof::nameof_enum require enum type.");
return {}; // Enum variable out of range NAMEOF_ENUM_RANGE.
}
};
template <typename T, typename = std::enable_if_t<!std::is_reference_v<T>>>
[[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 <typename T, typename = std::enable_if_t<!std::is_reference_v<T>>>
} // 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 <typename T, typename = std::enable_if_t<std::is_enum_v<std::decay_t<T>>>>
[[nodiscard]] constexpr std::string_view nameof_enum(T value) noexcept {
constexpr bool s = std::is_signed_v<std::underlying_type_t<std::decay_t<T>>>;
constexpr int min = s ? -NAMEOF_ENUM_MAX_SEARCH_DEPTH : 0;
constexpr int min = s ? -NAMEOF_ENUM_RANGE : 0;
if (static_cast<int>(value) >= NAMEOF_ENUM_RANGE || static_cast<int>(value) <= -NAMEOF_ENUM_RANGE) {
return {}; // Enum variable out of range NAMEOF_ENUM_RANGE.
}
return detail::nameof_enum_impl_t<std::decay_t<T>, min>{}(static_cast<int>(value));
}
// nameof_enum used to obtain the simple (unqualified) string enum name of static storage enum variable.
// nameof_enum<enum>() used to obtain the simple (unqualified) string enum name of static storage enum variable.
template <auto V, typename = std::enable_if_t<std::is_enum_v<std::decay_t<decltype(V)>>>>
[[nodiscard]] constexpr std::string_view nameof_enum() noexcept {
return detail::nameof_enum_impl<V>();
}
// nameof_type used to obtain the string name of type.
// nameof_type<type>() used to obtain the string name of type.
template <typename T>
[[nodiscard]] constexpr std::string_view nameof_type() noexcept {
return detail::nameof_type_impl<detail::identity<T>>();
@ -269,17 +264,20 @@ template <typename T>
// NAMEOF used to obtain the simple (unqualified) string name of variable, function, enum, macro.
#define NAMEOF(...) ::nameof::detail::nameof_impl<decltype(__VA_ARGS__)>(#__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<decltype(__VA_ARGS__)>(#__VA_ARGS__, true)
// NAMEOF_ENUM used to obtain the simple (unqualified) string enum name of enum variable.
#define NAMEOF_ENUM(...) ::nameof::nameof_enum<decltype(__VA_ARGS__)>(__VA_ARGS__)
// NAMEOF_TYPE used to obtain the string name of variable type.
#define NAMEOF_TYPE(...) ::nameof::nameof_type<decltype(__VA_ARGS__)>()
// 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<decltype(__VA_ARGS__)>()
// NAMEOF_RAW used to obtain the raw string name of variable, function, enum, macro.
#define NAMEOF_RAW(...) ::nameof::detail::nameof_raw_impl(#__VA_ARGS__)

View file

@ -23,7 +23,7 @@
#define CATCH_CONFIG_MAIN
#include <catch.hpp>
#define NAMEOF_ENUM_MAX_SEARCH_DEPTH 120
#define NAMEOF_ENUM_RANGE 120
#include <nameof.hpp>
#include <string>
@ -50,13 +50,13 @@ template <typename T>
class SomeClass {
public:
void SomeMethod5() const {
std::cout << NAMEOF_TYPE_T(T) << std::endl;
std::cout << NAMEOF_TYPE(T) << std::endl;
}
template <typename C>
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<T>(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<T>(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<value>()") {
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<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<Color::RED>() == "RED");
REQUIRE(nameof::nameof_enum<color>() == "RED");
@ -273,117 +297,117 @@ 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_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<int> *");
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<int>>()) == "const SomeClass<int> &&");
#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<int> 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<const SomeClass<int>>()) == "SomeClass<int> 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<int>*");
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<int>>()) == "const SomeClass<int>&&");
#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<int> *");
REQUIRE(NAMEOF_TYPE(SomeClass<int>) == "SomeClass<int>");
REQUIRE(NAMEOF_TYPE(const SomeClass<int> volatile *) == "const volatile SomeClass<int> *");
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<int>>()) == "const SomeClass<int> &&");
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<int> const volatile *");
REQUIRE(NAMEOF_TYPE(SomeClass<int>) == "SomeClass<int>");
REQUIRE(NAMEOF_TYPE(const SomeClass<int> volatile *) == "SomeClass<int> 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<const SomeClass<int>>()) == "SomeClass<int> 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<int>*");
REQUIRE(NAMEOF_TYPE(SomeClass<int>) == "SomeClass<int>");
REQUIRE(NAMEOF_TYPE(const SomeClass<int> volatile *) == "const volatile SomeClass<int>*");
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<int>>()) == "const SomeClass<int>&&");
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<int>) == "SomeClass<int>");
REQUIRE(NAMEOF_TYPE_T(const SomeClass<int> volatile *) == "const volatile SomeClass<int> *");
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<int>) == "SomeClass<int>");
REQUIRE(NAMEOF_TYPE_T(const SomeClass<int> volatile *) == "SomeClass<int> 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<int>) == "SomeClass<int>");
REQUIRE(NAMEOF_TYPE_T(const SomeClass<int> volatile *) == "const volatile SomeClass<int>*");
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<T>()"){
TEST_CASE("nameof::nameof_type<type>()"){
#if defined(__clang__)
REQUIRE(nameof::nameof_type<decltype(struct_var)>() == "SomeStruct");
REQUIRE(nameof::nameof_type<decltype(ptr_s)>() == "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");
}
}