This link states that the second example produces a dangling pointer. How is a dangling pointer created in the second expression but not the first? What is character s after the string?
std::string_view good("a string literal"); // OK: "good" points to a static array std::string_view bad("a temporary string"s); // "bad" holds a dangling pointer
sconstructs a temporary
std::stringfrom the string literal. Once the execution reaches the semicolon, the temporary is destroyed and a dangling pointer is left in the
sthere is a user-defined literal operator that produces a
The difference between the two lines is that the
goodone is a
string_viewpointing to a string literal, and string literals have static lifetime (they last for the whole problem). The
badone is a
string_viewpointing to a temporary
string, and that temporary owns its data - so when the temporary is destroyed (at the end of the line) it takes its data with it, and we end up with
badpointing to destroyed memory.