storage duration of underlying character data with user

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? ie 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.)


From [lex.ext]:

If L is a user-defined-string-literal, let str be the literal without its ud-suffix and let len be the number of code units in str (ie, its length excluding the terminating null character). The literal L is treated as a call of the form:

operator "" X (str , len )

From [lex.string]:

Evaluating a string-literal results in a string literal object with static storage duration , initialized from the given characters as specified above.

So:

"hello"_string;

is equivalent to:

operator "" _string("hello", 5)

As "hello" is a string-literal, it has static storage duration, so you will have no dangling pointer.

链接地址: http://www.djcxy.com/p/88012.html

上一篇: html标记的id中的特殊字符

下一篇: 存储用户基础字符数据的时间