c++ - std::string 在 C++11 中是否总是以空值结尾?

标签 c++ string c++11 language-lawyer null-terminated

Herb Sutter 在 2008 年在他的网站上发表的一篇文章中指出:

There is an active proposal to tighten this up further in C++0x and require null-termination and possibly ban copy-on-write implementations, for concurrency-related reasons. Here’s the paper: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2534.html . I think that one or both of the proposals in this paper is likely to be adopted, but we’ll see at the next meeting or two.

我知道 C++11 现在保证 std::string 内容连续存储,但他们在最终草案中采用了上述内容吗?

现在使用 &str[0] 之类的东西安全吗?

最佳答案

是的。根据 C++0x FDIS 21.4.7.1/1,std::basic_string::c_str() 必须返回

a pointer p such that p + i == &operator[](i) for each i in [0,size()].

这意味着给定一个字符串ss.c_str()返回的指针必须和字符串中起始字符的地址相同(&s[0]).

关于c++ - std::string 在 C++11 中是否总是以空值结尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6077189/

相关文章:

c++ - 为什么我没有得到所需程序的输出?

c++ - 循环在此代码中仅迭代一次,而在另一代码中它正确迭代

java - 循环不会多次迭代 if 语句

javascript - 如何在jquery中获取最终粘贴的文本?

C++11 延迟 "thread"创建(即指定线程函数但不等待创建物理线程)

c++ - 外部模板和不完整类型

c++ - 如何捕获 X 中的任意键?

string - Drupal8 Twig - 将字符串转换为整数

c++ - 为什么 g++ 与 -O3 段错误与以下 LLVM 库代码

c++ - 如何在运行控制台时更改文本