Quick setup: I want to pass strings around in my program as a pointer and a size. I have a String class and a user-defined literal for constructing literal Strings:
struct String { const char *ptr; size_t sz; };
inline constexpr String operator "" _string(const char *s, size_t sz) {
return {s, sz};
}
int main() {
auto s = "hello"_string;
s.ptr[0]; //<-- is this access guaranteed to work?
}
Does the standard specify that the argument passed to my user-defined literal operator has static duration? i.e. is the above code actually equivalent to writing:
int main() {
String s{"hello", 5};
}
or is the compiler/linker allowed to leave me with a dangling pointer when I use the user-defined literal?
(Section 2.13.8 of N4527 did not seem to say anything on the subject of storage class of the argument to the user-defined string literal operators. Any pointers into the appropriate section(s) of the standard would be appreciated.)