From d06a1c717a5ddd2827a9fbd85e72ca3ac94e2dee Mon Sep 17 00:00:00 2001 From: neargye Date: Thu, 24 Oct 2019 16:18:22 +0500 Subject: [PATCH] add c_str(), to_string_view(), explicit operator const char* --- example/example.cpp | 7 ++++--- include/nameof.hpp | 6 +++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index d65b933..9729673 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -83,7 +83,7 @@ SomeStruct structvar; Long othervar; SomeStruct* ptrvar = &structvar; -void name_to_str(const char* name) { +void name_to_chars(const char* name) { std::cout << name << std::endl; } @@ -98,9 +98,10 @@ void name_to_string_view(std::string_view name) { int main() { // Compile-time. constexpr auto name = NAMEOF(structvar); - static_assert("structvar" == name); + using namespace std::literals::string_view_literals; + static_assert("structvar"sv == name); - name_to_str(name.data()); // 'structvar' + name_to_chars(name.c_str()); // 'structvar' name_to_string(std::string{name}); // 'structvar' name_to_string_view(name); // 'structvar' diff --git a/include/nameof.hpp b/include/nameof.hpp index 29fb37d..34ff704 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -162,9 +162,13 @@ class [[nodiscard]] cstring { return std::string_view{data(), size()}.compare(str); } + [[nodiscard]] constexpr std::string_view to_string_view() const noexcept { return {data(), size()}; } + + [[nodiscard]] constexpr const char* c_str() const noexcept { return data(); } + [[nodiscard]] constexpr operator std::string_view() const noexcept { return {data(), size()}; } - + [[nodiscard]] constexpr explicit operator const char*() const noexcept { return data(); } }; template