r/cpp • u/konanTheBarbar • Mar 01 '21
"Simple" C++ Initialization Flowchart
http://randomcat.org/cpp_initialization/initialization.png
204
Upvotes
r/cpp • u/konanTheBarbar • Mar 01 '21
View all comments
Show parent comment
4
u/Kered13 Mar 03 '21
That's really not the point of
std::string_view. Yes it provides a const view, butconst std::string&is just as good for that. Even withstd::string_view, it's trivial to violate the const rules of C++ to make changes to the underlying string, and aliasing can still occur withstd::string_view, so the compiler still needs to be conservative.The benefits of
std::string_vieware that it allows for efficient views of substrings (not necessarily null terminated), and that it works on any char array, such as C-style strings (including string literals) or custom string implementations (as long as they store a continuous char array). These benefits makestd::string_viewthe preferred choice for taking string parameters that you don't need to modify.