4 ms·
It could be made safe by making to_string_t a template and forwarding from_string's argument into its storage, if things are not complicated enough already :)
by ot 6y ago
It could be made safe by making to_string_t a template and forwarding from_string's argument into its storage, if things are not complicated enough already :)
Something like this
template <class String>
struct to_string_t {
const String s;
...
}
template <class String>
auto from_string(String&& s) { return to_string_t<String>{std::forward<String>(s)}; }
Then the temporary's ownership would be passed to to_string_t. Would still have surprising behavior if a the argument is a non-const reference though.
- MaulingMonkey 6y agoIt'd help with the edge case I showed... "safe" is a strong word though, doesn't handle this one: std::string json() { return "\"123\""; } std::string_view unquote(std::string_view s) { return { s.data()+1, s.size()-2 }; } auto i = from_string(unquote(json())); It's a fundamental problem of subtly & unexpectedly deferred action.
- ot 6y agoI meant to write "safer" but the "r" fell through :)