Last active
May 15, 2024 08:19
-
-
Save tuket/d4c09344a2173b1acc1651d416733fbd to your computer and use it in GitHub Desktop.
proposal for ztring_view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <string_view> | |
struct zstring_view : public std::string_view { | |
zstring_view() {} | |
zstring_view(std::string_view s) = delete; | |
zstring_view(const char* s) : std::string_view(s) {} | |
zstring_view(const std::string& s) : std::string_view(s) {} | |
operator const char* ()const { return data(); } | |
operator std::string()const { return std::string(begin(), end()); } | |
const char* c_str()const { return data(); } | |
}; | |
void foo_string(const std::string& s) {} | |
void foo_string_view(std::string_view s) {} | |
int main() | |
{ | |
std::string myString = "myString"; | |
std::string_view myStringView = "myStringView"; | |
zstring_view a("a"); | |
zstring_view b; | |
b = myString; | |
b = a; | |
//zstring_view c = myStringView; // ERROR | |
// b = myStringView; // ERROR | |
foo_string(a); | |
foo_string_view(a); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment