在c++ 11中,字符串总是以null结尾吗?

时间:2022-11-02 20:40:34

In a 2008 post on his site, Herb Sutter states the following:

2008年,Herb Sutter在他的网站上发表了一篇文章,他写道:

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++ 0x中进一步加强这一功能,并要求以与并发相关的原因,终止并可能禁止写复制的实现。这是论文:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2534.html。我认为这篇论文中的一个或两个建议可能会被通过,但我们将在下次会议上看到。

I know that C++11 now guarantees that the std::string contents get stored contiguously, but did they adopt the above in the final draft?

我知道c++ 11现在保证std::string内容被连续存储,但是他们在最终稿中采用了上面的方法吗?

Will it now be safe to use something like &str[0]?

现在使用像&str[0]这样的东西安全吗?

1 个解决方案

#1


46  

Yes. Per the C++0x FDIS 21.4.7.1/1, std::basic_string::c_str() must return

是的。对于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()].

在[0,size()]中,每个i的指针p + i ==和运算符[](i)。

This means that given a string s, the pointer returned by s.c_str() must be the same as the address of the initial character in the string (&s[0]).

这意味着给定一个字符串s, s.c str()返回的指针必须与字符串(&s[0])中初始字符的地址相同。

#1


46  

Yes. Per the C++0x FDIS 21.4.7.1/1, std::basic_string::c_str() must return

是的。对于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()].

在[0,size()]中,每个i的指针p + i ==和运算符[](i)。

This means that given a string s, the pointer returned by s.c_str() must be the same as the address of the initial character in the string (&s[0]).

这意味着给定一个字符串s, s.c str()返回的指针必须与字符串(&s[0])中初始字符的地址相同。