From e59687da2a86fac5686d71f702703f81b7704809 Mon Sep 17 00:00:00 2001 From: Neargye Date: Mon, 6 Aug 2018 17:42:46 +0500 Subject: [PATCH] fix test fix constexpr, rvalue --- include/nameof.hpp | 26 ++++++++++++++------------ test/test.cpp | 12 ++++++------ 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index fa1e7d7..a8dd3d3 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -35,8 +35,10 @@ #include #include -#if !(__cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L)) -#error "Request C++14." +#if (__cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L)) +# define NAMEOF_CONSTEXPR14 constexpr +#else +# define NAMEOF_CONSTEXPR14 #endif namespace nameof { @@ -116,7 +118,7 @@ class cstring final { return {str_ + pos, n}; } - inline friend constexpr bool operator==(const cstring& lhs, const cstring& rhs) noexcept { + inline friend NAMEOF_CONSTEXPR14 bool operator==(const cstring& lhs, const cstring& rhs) noexcept { if (lhs.size_ != rhs.size_) { return false; } @@ -130,17 +132,17 @@ class cstring final { return true; } - inline friend constexpr bool operator!=(const cstring& lhs, const cstring& rhs) noexcept { + inline friend NAMEOF_CONSTEXPR14 bool operator!=(const cstring& lhs, const cstring& rhs) noexcept { return !(lhs == rhs); } template - inline friend constexpr bool operator==(const cstring& lhs, const char(&str)[N]) noexcept { + inline friend NAMEOF_CONSTEXPR14 bool operator==(const cstring& lhs, const char(&str)[N]) noexcept { return lhs == cstring{str, N - 1}; } template - inline friend constexpr bool operator!=(const cstring& lhs, const char(&str)[N]) noexcept { + inline friend NAMEOF_CONSTEXPR14 bool operator!=(const cstring& lhs, const char(&str)[N]) noexcept { return !(lhs == cstring{str, N - 1}); } @@ -156,7 +158,7 @@ inline constexpr bool IsLexeme(char s) noexcept { return !((s >= '0' && s <= '9') || (s >= 'a' && s <= 'z') || (s >= 'A' && s <= 'Z') || s == '_'); } -inline constexpr cstring NameofBase(const char* name, std::size_t length, bool with_suffix) noexcept { +inline NAMEOF_CONSTEXPR14 cstring NameofBase(const char* name, std::size_t length, bool with_suffix) noexcept { std::size_t p = 0; if(IsLexeme(name[length - 1])) { for (std::size_t i = length, h = 0; i > 0; --i) { @@ -206,7 +208,7 @@ inline constexpr cstring NameofRaw(const char* name, std::size_t length) noexcep template ::value && !std::is_void::value>::type> -inline constexpr detail::cstring Nameof(const T&, const char* name, std::size_t length, bool with_suffix = false) noexcept { +inline NAMEOF_CONSTEXPR14 detail::cstring Nameof(const T&, const char* name, std::size_t length, bool with_suffix = false) noexcept { return detail::NameofBase(name, length, with_suffix); } @@ -214,10 +216,10 @@ template ::value && !std::is_function::value && !std::is_member_function_pointer::value>::type> -inline constexpr detail::cstring Nameof(T&&, const char*, std::size_t) = delete; +inline NAMEOF_CONSTEXPR14 detail::cstring Nameof(T&&, const char*, std::size_t, bool) = delete; template -inline constexpr detail::cstring NameofTypeRaw() noexcept { +inline NAMEOF_CONSTEXPR14 detail::cstring NameofTypeRaw() noexcept { #if defined(__clang__) const auto function_name = __PRETTY_FUNCTION__; const auto total_length = sizeof(__PRETTY_FUNCTION__) - 1; @@ -239,7 +241,7 @@ inline constexpr detail::cstring NameofTypeRaw() noexcept { } template ::type> -inline constexpr detail::cstring NameofType() noexcept { +inline NAMEOF_CONSTEXPR14 detail::cstring NameofType() noexcept { const auto raw_type_name = NameofTypeRaw(); return detail::NameofBase(raw_type_name.begin(), raw_type_name.length(), false); } @@ -255,7 +257,7 @@ inline constexpr detail::cstring NameofType() noexcept { #endif // Used to obtain the simple (unqualified) string name of a variable, member, function. -#define NAMEOF(name) ::nameof::Nameof(name, #name, (sizeof(#name) / sizeof(char)) - 1) +#define NAMEOF(name) ::nameof::Nameof(name, #name, (sizeof(#name) / sizeof(char)) - 1, false) // Used to obtain the full string name of a variable, member, function. #define NAMEOF_FULL(name) ::nameof::Nameof(name, #name, (sizeof(#name) / sizeof(char)) - 1, true) diff --git a/test/test.cpp b/test/test.cpp index 37c721c..4a29b5b 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -75,7 +75,7 @@ Long othervar; SomeStruct& refvar = somevar; SomeStruct* ptrvar = &somevar; -#if 0 && (__cplusplus >= 201402L || (defined(_MSVC_LANG ) && _MSVC_LANG >= 201402L)) +#if (__cplusplus >= 201402L || (defined(_MSVC_LANG ) && _MSVC_LANG >= 201402L)) // Compile-time supported by C++14. TEST_CASE("constexpr") { SECTION("NAMEOF") { @@ -138,7 +138,7 @@ TEST_CASE("constexpr") { static_assert(cx1 == "class SomeClass", ""); static_assert(cx2 == "class SomeClass", ""); static_assert(cx3 == "class SomeClass", ""); -#else +#elif defined(__GNUC__) || defined(__clang__) static_assert(cx1 == "SomeClass", ""); static_assert(cx2 == "SomeClass", ""); static_assert(cx3 == "SomeClass", ""); @@ -262,7 +262,7 @@ TEST_CASE("type raw name") { REQUIRE(NAMEOF_TYPE_RAW(Color::RED) == "Color"); - REQUIRE(NAMEOF_TYPE_RAW(std::declval>()) == "const SomeClass&&"); + REQUIRE(NAMEOF_TYPE_RAW(std::declval>()) == "const SomeClass &&"); #elif defined(__GNUC__) REQUIRE(NAMEOF_TYPE_RAW(somevar) == "SomeStruct"); REQUIRE(NAMEOF_TYPE_RAW(ptrvar) == "SomeStruct*"); @@ -274,7 +274,7 @@ TEST_CASE("type raw name") { REQUIRE(NAMEOF_TYPE_RAW(Color::RED) == "Color"); - REQUIRE(NAMEOF_TYPE_RAW(std::declval>()) == "const SomeClass &&"); + REQUIRE(NAMEOF_TYPE_RAW(std::declval>()) == "const SomeClass&&"); #endif } @@ -285,7 +285,7 @@ TEST_CASE("Spaces and Tabs ignored") { REQUIRE(NAMEOF_TYPE( somevar ) == "SomeStruct"); #if defined(_MSC_VER) REQUIRE(NAMEOF_TYPE_RAW( somevar ) == "struct SomeStruct"); -#else +#elif defined(__GNUC__) || defined(__clang__) REQUIRE(NAMEOF_TYPE_RAW( somevar ) == "SomeStruct"); #endif } @@ -296,7 +296,7 @@ TEST_CASE("Spaces and Tabs ignored") { REQUIRE(NAMEOF_TYPE( somevar ) == "SomeStruct"); #if defined(_MSC_VER) REQUIRE(NAMEOF_TYPE_RAW( somevar ) == "struct SomeStruct"); -#else +#elif defined(__GNUC__) || defined(__clang__) REQUIRE(NAMEOF_TYPE_RAW( somevar ) == "SomeStruct"); #endif }