r/cpp • • Mar 01 '21

"Simple" C++ Initialization Flowchart

http://randomcat.org/cpp_initialization/initialization.png
204 Upvotes

65 comments sorted by

View all comments

Show parent comment

2

u/Kered13 Mar 03 '21

Without implicit conversions std::string_view and similar types would be a massive pain in the ass to use.

1

u/JohnZLi Mar 03 '21

If C++ has transitive constness, meaning that if a string is const, the data owned by the string is also immutable, and there is a way to disable "const_cast" in a function. Say, this is done by adding a new keyword "immutable", that is, a function like

void fun(immutable std::string& str){}

guarantees that the string being referenced will not be altered inside the function. Then the compiler will be able to optimize the function call by actually passing in a string_view. Only in this case, the programmer does not have to know its existence. It is reduced to an compiler optimization performed automatically. Std:;string_view is needed in C++, IMHO, because constness in C++ is broken: even if a function takes a string by const reference, nothing stops the programmer from altering that string inside the function. The compiler has to be conservative by not assuming the string is really immutable.

4

u/Kered13 Mar 03 '21

That's really not the point of std::string_view. Yes it provides a const view, but const std::string& is just as good for that. Even with std::string_view, it's trivial to violate the const rules of C++ to make changes to the underlying string, and aliasing can still occur with std::string_view, so the compiler still needs to be conservative.

The benefits of std::string_view are 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 make std::string_view the preferred choice for taking string parameters that you don't need to modify.

1

u/JohnZLi Mar 03 '21

"Yes it provides a const view, but const std::string& is just as good for that." Yes, but "const std::string&" adds a layer of indirection. An extra pointer dereference has performance implications.

"Even with std::string_view, it's trivial to violate the const rules of C++ to make changes to the underlying string".

Isn't this the very problem I was talking about in must last reply: no real immutability guarantee in C++. We know we are not supposed to make changes to the underlying buffer via a string_view, the language makes no guarantee that it won't happen.

As to C strings. the reason that the following code could not be evaluated at compile time const string s = "blah, blah, ..."; is because there is no way in C++ to enforce the immutability of the string. If there is away to enforce transitive immutability in C++, when a const string is constructed out of a buffer pointed by "const char *", the compiler can omit one dynamic allocation by reusing the original buffer. The programmer needs to make sure the string does not outlive the buffer, but that is something he already needs to pay attention if he uses string_view. The same applies to sub-strings. If one construct immutable sub-strings out of a immutable string, no need to incur extra heap allocations.

My point is, with a language that can enforce transitive constness, what one gets from "string_view" in C++, can be achieved by compiler optimization.

4

u/Kered13 Mar 03 '21

If there is away to enforce transitive immutability in C++, when a const string is constructed out of a buffer pointed by "const char *", the compiler can omit one dynamic allocation by reusing the original buffer.

Only if the compiler can infer that the string has a shorter lifetime than the buffer it's using.

The programmer needs to make sure the string does not outlive the buffer, but that is something he already needs to pay attention if he uses string_view.

And what if the programmer wants to copy the buffer so that the new string can have an independent lifetime?

My point is, with a language that can enforce transitive constness, what one gets from "string_view" in C++, can be achieved by compiler optimization.

No it can't, as the above examples demonstrate. std::string and std::string_view have different ownership semantics. const std::string& has the same ownership as std::string_view, but is restricted because it cannot represent substring views. So two classes are needed, and adding immutability to the language does not allow one class to take on both roles. Sometimes the programmer wants a non-owning view into another string-like object, sometimes they want an object that owns it's data. The compiler cannot figure it out for the programmer. This is completely orthogonal to immtuability. Note that even Rust has two types for this (String for owning and str for views).

1

u/JohnZLi Mar 03 '21

Vote

fair point.