From 654c74d5db8bb9523f386da00a43493610176502 Mon Sep 17 00:00:00 2001 From: Neargye Date: Mon, 6 Aug 2018 19:53:58 +0500 Subject: [PATCH] clean-up --- CMakeLists.txt | 2 +- example/example.cpp | 2 +- include/nameof.hpp | 19 ++++++++----------- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 63bbcca..2a9217d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,8 +7,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) if((CMAKE_CXX_COMPILER_ID MATCHES "GNU") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang")) - add_compile_options(-stdlib=libc++) add_compile_options(-Wall -Wextra -pedantic-errors) + set(CMAKE_VERBOSE_MAKEFILE ON) elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") add_compile_options(/W4) check_cxx_compiler_flag(/permissive HAS_PERMISSIVE_FLAG) diff --git a/example/example.cpp b/example/example.cpp index c1d7248..79639b1 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -86,7 +86,7 @@ int main() { #if (__cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSC_VER >= 1910 && _MSVC_LANG >= 201402L)) // Compile-time supported by C++14. constexpr auto constexpr_work_fine = NAMEOF(somevar); - std::cout << constexpr_work_fine << std::endl; // somevar + static_assert(constexpr_work_fine == "somevar", ""); #endif // Enum name. diff --git a/include/nameof.hpp b/include/nameof.hpp index f6ed218..f01e287 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -32,7 +32,7 @@ #include #include -#include +#include #include #if (__cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSC_VER >= 1910 && _MSVC_LANG >= 201402L)) @@ -59,6 +59,10 @@ struct remove_all_pointers template using Decay = std::remove_reference::type>::type>; +inline constexpr bool StrEquals(const char* lhs, const char* rhs, std::size_t size) { + return size == 0 ? (lhs[0] == rhs[0]) : ((lhs[size - 1] == rhs[size - 1]) && StrEquals(lhs, rhs, size - 1)); +} + // STD like compile-time string. class cstring final { const char* str_; @@ -83,8 +87,6 @@ class cstring final { inline constexpr std::size_t length() const noexcept { return size_; } - inline constexpr std::size_t max_size() const noexcept { return std::numeric_limits::max(); } - inline constexpr bool empty() const noexcept { return size_ == 0; } inline constexpr const char* begin() const noexcept { return str_; } @@ -116,7 +118,7 @@ class cstring final { } inline friend constexpr bool operator==(const cstring& lhs, const cstring& rhs) noexcept { - return (lhs.size_ == rhs.size_) && equals(lhs.begin(), rhs.begin(), lhs.size()); + return (lhs.size_ == rhs.size_) && StrEquals(lhs.begin(), rhs.begin(), lhs.size()); } inline friend constexpr bool operator!=(const cstring& lhs, const cstring& rhs) noexcept { @@ -125,12 +127,12 @@ class cstring final { template inline friend constexpr bool operator==(const cstring& lhs, const char(&str)[N]) noexcept { - return lhs == cstring{str, N - 1}; + return (lhs.size_ == N - 1) && StrEquals(lhs.begin(), str, lhs.size()); } template inline friend constexpr bool operator!=(const cstring& lhs, const char(&str)[N]) noexcept { - return lhs != cstring{str, N - 1}; + return !(lhs == str); } inline friend std::ostream& operator<<(std::ostream& os, const cstring& str) { @@ -139,11 +141,6 @@ class cstring final { } inline operator std::string() const { return std::string(begin(), size()); } - - private: - static inline constexpr bool equals(const char* lhs, const char* rhs, std::size_t size) { - return size == 0 ? (lhs[0] == rhs[0]) : ((lhs[size - 1] == rhs[size - 1]) && equals(lhs, rhs, size - 1)); - } }; inline constexpr bool IsLexeme(char s) noexcept {