Is there a reason to keep using C strings?
I think it would be fair to say that, apart from talking to the C API, there has never been a reason to use C strings.
When designing an interface for a function or method that just needs read-only characters, you need to prefer std::string_view . For instance. string search, uppercase copies, printing, etc.
When designing an interface that takes a copy of a character string, you should probably prefer the first and last iterators. However, std::string_view can be considered as a proxy for these iterators, so string_view is suitable.
If you want to take responsibility for a long string, you probably prefer to skip std::string by value or by r-value reference.
When designing an object that marshals access the c API, which expects null-terminated strings, you should prefer std :: string or std :: string const & - because its c_str () method correctly returns a null-terminated string.
When storing strings in objects (which are not temporary proxies), prefer std :: string.
Of course, using const char* as the data owner in C ++ never works. There is always a better way. This has been true since C ++ 98.
source share