diff --git a/include/nameof.hpp b/include/nameof.hpp index 2032c3d..857b4fe 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -866,7 +866,7 @@ string nameof_type_rtti(const char* tn) { const auto dmg = abi::__cxa_demangle(tn, nullptr, nullptr, nullptr); const auto name = string{dmg}; free(dmg); - assert(name.size() > 0 && "Type does not have a name."); + assert(!name.empty() && "Type does not have a name."); return name; } @@ -877,7 +877,7 @@ string nameof_full_type_rtti(const char* tn) { const auto dmg = abi::__cxa_demangle(tn, nullptr, nullptr, nullptr); auto name = string{dmg}; free(dmg); - assert(name.size() > 0 && "Type does not have a name."); + assert(!name.empty() && "Type does not have a name."); if constexpr (std::is_const_v>) { name = string{"const "}.append(name); } @@ -901,7 +901,7 @@ string nameof_short_type_rtti(const char* tn) { const auto pname = pretty_name(dmg); const auto name = string{pname.data(), pname.size()}; free(dmg); - assert(name.size() > 0 && "Type does not have a short name."); + assert(!name.empty() && "Type does not have a short name."); return name; } @@ -910,16 +910,16 @@ template string nameof_type_rtti(const char* tn) noexcept { static_assert(nameof_type_rtti_supported::value, "nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); const auto name = string_view{tn}; - assert(name.size() > 0 && "Type does not have a name."); + assert(!name.empty() && "Type does not have a name."); - return {name.begin(), name.end()}; + return {name.data(), name.size()}; } template string nameof_full_type_rtti(const char* tn) noexcept { static_assert(nameof_type_rtti_supported::value, "nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); auto name = string{tn}; - assert(name.size() > 0 && "Type does not have a name."); + assert(!name.empty() && "Type does not have a name."); if constexpr (std::is_const_v>) { name = string{"const "}.append(name); } @@ -940,9 +940,9 @@ template = 0> string nameof_short_type_rtti(const char* tn) noexcept { static_assert(nameof_type_rtti_supported::value, "nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); const auto name = pretty_name(tn); - assert(name.size() > 0 && "Type does not have a short name."); + assert(!name.empty() && "Type does not have a short name."); - return {name.begin(), name.end()}; + return {name.data(), name.size()}; } #endif @@ -1084,7 +1084,7 @@ template using D = std::decay_t; static_assert(detail::nameof_enum_supported::value, "nameof::nameof_enum unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); constexpr string_view name = detail::enum_name_v; - static_assert(name.size() > 0, "Enum value does not have a name."); + static_assert(!name.empty(), "Enum value does not have a name."); return name; } @@ -1095,7 +1095,7 @@ template static_assert(detail::nameof_type_supported::value, "nameof::nameof_type unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); using U = detail::identity>; constexpr string_view name = detail::type_name_v; - static_assert(name.size() > 0, "Type does not have a name."); + static_assert(!name.empty(), "Type does not have a name."); return name; } @@ -1106,7 +1106,7 @@ template static_assert(detail::nameof_type_supported::value, "nameof::nameof_type unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); using U = detail::identity; constexpr string_view name = detail::type_name_v; - static_assert(name.size() > 0, "Type does not have a full name."); + static_assert(!name.empty(), "Type does not have a full name."); return name; } @@ -1117,7 +1117,7 @@ template static_assert(detail::nameof_type_supported::value, "nameof::nameof_type unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); using U = detail::identity>; constexpr string_view name = detail::pretty_name(detail::type_name_v); - static_assert(name.size() > 0, "Type does not have a short name."); + static_assert(!name.empty(), "Type does not have a short name."); return name; } @@ -1127,7 +1127,7 @@ template [[nodiscard]] constexpr auto nameof_member() noexcept -> std::enable_if_t, string_view> { static_assert(detail::nameof_member_supported::value, "nameof::nameof_memder unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); constexpr string_view name = detail::member_name_v; - static_assert(name.size() > 0, "Member does not have a name."); + static_assert(!name.empty(), "Member does not have a name."); return name; } @@ -1135,30 +1135,30 @@ template } // namespace nameof // Obtains name of variable, function, macro. -#define NAMEOF(...) []() constexpr noexcept { \ - ::std::void_t(); \ - constexpr auto _name = ::nameof::detail::pretty_name(#__VA_ARGS__); \ - static_assert(_name.size() > 0, "Expression does not have a name."); \ - constexpr auto _size = _name.size(); \ - constexpr auto _nameof = ::nameof::cstring<_size>{_name}; \ +#define NAMEOF(...) []() constexpr noexcept { \ + ::std::void_t(); \ + constexpr auto _name = ::nameof::detail::pretty_name(#__VA_ARGS__); \ + static_assert(!_name.empty(), "Expression does not have a name."); \ + constexpr auto _size = _name.size(); \ + constexpr auto _nameof = ::nameof::cstring<_size>{_name}; \ return _nameof; }() // Obtains full name of variable, function, macro. #define NAMEOF_FULL(...) []() constexpr noexcept { \ ::std::void_t(); \ constexpr auto _name = ::nameof::detail::pretty_name(#__VA_ARGS__, false); \ - static_assert(_name.size() > 0, "Expression does not have a name."); \ + static_assert(!_name.empty(), "Expression does not have a name."); \ constexpr auto _size = _name.size(); \ constexpr auto _nameof_full = ::nameof::cstring<_size>{_name}; \ return _nameof_full; }() // Obtains raw name of variable, function, macro. -#define NAMEOF_RAW(...) []() constexpr noexcept { \ - ::std::void_t(); \ - constexpr auto _name = ::nameof::string_view{#__VA_ARGS__}; \ - static_assert(_name.size() > 0, "Expression does not have a name."); \ - constexpr auto _size = _name.size(); \ - constexpr auto _nameof_raw = ::nameof::cstring<_size>{_name}; \ +#define NAMEOF_RAW(...) []() constexpr noexcept { \ + ::std::void_t(); \ + constexpr auto _name = ::nameof::string_view{#__VA_ARGS__}; \ + static_assert(!_name.empty(), "Expression does not have a name."); \ + constexpr auto _size = _name.size(); \ + constexpr auto _nameof_raw = ::nameof::cstring<_size>{_name}; \ return _nameof_raw; }() // Obtains name of enum variable. diff --git a/test/test_aliases.cpp b/test/test_aliases.cpp index ed893e0..0f1d417 100644 --- a/test/test_aliases.cpp +++ b/test/test_aliases.cpp @@ -31,13 +31,11 @@ struct MyString { MyString(const char* s) : str{s} {} // required MyString(const char* s, std::size_t l) : str{s, l} {} // required bool empty() const { return str.empty(); } // required - std::size_t size() const { return str.size(); } // required - auto begin() const { return str.begin(); } // required - auto end() const { return str.end(); } // required MyString& append(std::size_t count, char c) { str.append(count, c); return *this; } // required MyString& append(const char* s) { str.append(s); return *this; } // required MyString& append(const MyString& s) { str.append(s.str); return *this; } // required + std::size_t size() const { return str.size(); } int compare(const char* s) const { return str.compare(s); } private: @@ -55,14 +53,9 @@ struct MyStringView { constexpr std::size_t size() const { return str.size(); } // required constexpr const char* data() const { return str.data(); } // required constexpr const char& operator[](std::size_t i) const { return str[i]; } // required - constexpr auto begin() const { return str.begin(); } // required - constexpr auto end() const { return str.end(); } // required - constexpr std::size_t find(char c) const { return str.find(c); } // required - constexpr MyStringView substr(std::size_t p, std::size_t n) { return str.substr(p, n); } // required constexpr void remove_prefix(std::size_t n) { str.remove_prefix(n); } // required constexpr void remove_suffix(std::size_t n) { str.remove_suffix(n); } // required constexpr int compare(MyStringView s) const { return str.compare(s.str); } // required - friend constexpr bool operator==(MyStringView lhs, MyStringView rhs); // required constexpr int compare(const char* s) const { return str.compare(s); } @@ -72,10 +65,6 @@ struct MyStringView { constexpr MyStringView(std::string_view s) : str{s} {} }; -constexpr bool operator==(MyStringView lhs, MyStringView rhs) { - return lhs.str == rhs.str; -} - #define NAMEOF_USING_ALIAS_STRING using string = MyString; #define NAMEOF_USING_ALIAS_STRING_VIEW using string_view = MyStringView;