He means the string value of the variable admin_id, not the variable name admin_id.
And the bug comes from the temporary string object created when he passes in the string to Sanitize.
Sanitize("foobarbaz") -> creates temporary string -> string_view from temporary string -> returns string_view of temporary string
temporary string destructed
operations on returned string_view -> oops, use after free.q
But if Sanitize has a return type of string_view, then returning the temporary variable should make a copy of it (or move it), right? And the string "foobarbazboo" is static so that's not being freed either.
And the while loop shouldn't even run at all since front() never equals ' '.
string_view is literally a reference. Copying a string_view just copies the reference.
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.