add c_str(), to_string_view(), explicit operator const char*

This commit is contained in:
neargye 2019-10-24 16:18:22 +05:00
parent e831936fbe
commit d06a1c717a
2 changed files with 9 additions and 4 deletions

View file

@ -83,7 +83,7 @@ SomeStruct structvar;
Long othervar; Long othervar;
SomeStruct* ptrvar = &structvar; SomeStruct* ptrvar = &structvar;
void name_to_str(const char* name) { void name_to_chars(const char* name) {
std::cout << name << std::endl; std::cout << name << std::endl;
} }
@ -98,9 +98,10 @@ void name_to_string_view(std::string_view name) {
int main() { int main() {
// Compile-time. // Compile-time.
constexpr auto name = NAMEOF(structvar); 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(std::string{name}); // 'structvar'
name_to_string_view(name); // 'structvar' name_to_string_view(name); // 'structvar'

View file

@ -162,9 +162,13 @@ class [[nodiscard]] cstring {
return std::string_view{data(), size()}.compare(str); 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 operator std::string_view() const noexcept { return {data(), size()}; }
[[nodiscard]] constexpr explicit operator const char*() const noexcept { return data(); }
}; };
template <std::size_t N> template <std::size_t N>