A string_view, in terms of C++, is 100% definitely not a reference. A string_view is not a reference but rather an object. It's an object of a type (namely, `string_view`) that happens to have what we sometimes call "view semantics" or "reference semantics," but it's absolutely not a reference itself. Nor, by the way, is it a pointer (and by the way, a pointer is also an object, not a reference).
But it's not actually taken as a reference (with &) anywhere I see... and even if it's just internally referencing the (static) string, there shouldn't be any issue with that memory either right?
> But it's not actually taken as a reference (with &) anywhere I see...
That is effectively what is happening internally. A string_view is morally a pointer/length pair; that pointer has to be initialized somehow.
> and even if it's just internally referencing the (static) string
It's not. The function call constructs a temporary std::string from the string literal, but once that is done the contents of the temporary are entirely independent from the string literal.
Where ref comes from the temporary string constructed via Sanitize("foobarbaz"). When that expression is done, the temporary string is destructed, leaving a dangling pointer.
A string_view, in terms of C++, is 100% definitely not a reference. A string_view is not a reference but rather an object. It's an object of a type (namely, `string_view`) that happens to have what we sometimes call "view semantics" or "reference semantics," but it's absolutely not a reference itself. Nor, by the way, is it a pointer (and by the way, a pointer is also an object, not a reference).
Possibly useful elaboration: https://quuxplusone.github.io/blog/2022/12/15/sophist/
In C++, where "reference" and "pointer" both have technical (jargon) meanings, you might still get away with calling string_view a "handle" (to a contiguous bunch of characters). See also https://quuxplusone.github.io/blog/2023/02/04/jargon-treadmi...
But it's not actually taken as a reference (with &) anywhere I see... and even if it's just internally referencing the (static) string, there shouldn't be any issue with that memory either right?
> But it's not actually taken as a reference (with &) anywhere I see...
That is effectively what is happening internally. A string_view is morally a pointer/length pair; that pointer has to be initialized somehow.
> and even if it's just internally referencing the (static) string
It's not. The function call constructs a temporary std::string from the string literal, but once that is done the contents of the temporary are entirely independent from the string literal.
string_view is something like this
Where ref comes from the temporary string constructed via Sanitize("foobarbaz"). When that expression is done, the temporary string is destructed, leaving a dangling pointer.