add c_str(), to_string_view(), explicit operator const char*
This commit is contained in:
parent
e831936fbe
commit
d06a1c717a
2 changed files with 9 additions and 4 deletions
|
@ -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'
|
||||
|
||||
|
|
|
@ -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 <std::size_t N>
|
||||
|
|
Loading…
Reference in a new issue