默认构造的std :: string c_str()值

发布于 2025-02-08 14:52:01 字数 536 浏览 1 评论 0 原文

std::string s1;
std::string s2;
assert(strlen(s1.c_str()) == 0);
assert(s1.c_str() == s2.c_str());

这两个断言总是正确的吗?

我使用C ++ 11,并且已经检查了标准,§21.4.2中的表63说:

data()一个可复制的非挂钩指针,可以将0添加到它

size()0

容量()未指定的值

我认为 c_str() data()相同。但是我对这种定义有一些疑问。

  1. 可以添加0个“ ==” 必须并且始终添加了0?

  2. 所有默认构造的std :: String是否共享相同的底层缓冲区?

我在GCC上测试,这两个断言是正确的。我想知道所有编译器总是正确的吗?

std::string s1;
std::string s2;
assert(strlen(s1.c_str()) == 0);
assert(s1.c_str() == s2.c_str());

Does these two assert always true?

I use C++11, and I have checked the standard, the Table 63 in §21.4.2 says:

data() a non-null pointer that is copyable and can have 0 added to it

size() 0

capacity() an unspecified value

I think c_str() is the same as data(). But I have some question about this defination.

  1. Does "CAN have 0 added to it" == "MUST and ALWAYS have 0 added to it"?

  2. Does all default constructed std::string shared a same underlay buffer?

I test on gcc, these two assert is true. I wonder does these always true for all compiler?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别念他 2025-02-15 14:52:01

第一断言可以成功。 c_str()始终将指针返回到null端接的字符串,该字符串与 std :: String :: String 对象相同的字符串内容,这是两个<<代码> S1

不能保证第二个断言能成功。如果内容相同,则没有什么需要 c_str() std :: string 返回的。默认结构的字符串不需要共享相同的基础缓冲区。那将是特定标准库实施的实施细节。 (我认为libstdc ++会根据配置做出一些反兼容(?)原因(?)的原因,如果我记得正确,请参见 - 启用 - dynamic-string configure option)。

请注意,在C ++ 11之前, data() did 不是的效果与 c_str() data()不能保证给指向零端的字符串的指针。如果字符串是空的,则不允许将其返回的指针被删除。因此,用 c_str() data()在您的示例中,在C ++ 11之前,会导致对 strlen 。


措辞“ and可以将0添加到它”有些怪异,我不确定它应该传达什么,但是对于C ++ 11(草稿N3337) data() 的返回值在这样, data() + i ==&amp; operator [](i)在范围内 in范围 [0, size()] operator [] [strings.access]/2 返回 Chart()(aka a aka a null cartem)的参考)没有任何条件。

奇怪的措辞也已通过2018年的社论更改替换,请参见 https://github.com/ cplusplus/draft/pult/1879

The first assertion is guaranteed to succeed. c_str() always returns a pointer to a null-terminated string with the same string contents as held by the std::string object, which is an empty string for both s1.

The second assertion is not guaranteed to succeed. There is nothing requiring the c_str() returned from a std::string to be the same if the content is the same. Default-constructed strings do not need to share the same underlying buffer. That would be an implementation detail of a particular standard library implementation. (I think libstdc++ does something like this depending on configuration for some backwards-compatibility(?) reasons if I remember correctly, see the --enable-fully-dynamic-string configure option).

Note that prior to C++11, data() did not have the same effect as c_str(). data() was not guaranteed to give a pointer to a null-terminated string. If the string was empty, then the pointer returned by it was not allowed to be dereferenced. So replacing c_str() with data() in your examples would, prior to C++11, result in undefined behavior on the call to strlen.


The wording "and can have 0 added to it" is somewhat weird and I am not completely sure what it is supposed to convey, but for C++11 (draft N3337) data()'s return value is further specified in [string.accessors]/1 so that data() + i == &operator[](i) for all i in the range [0,size()] and operator[] is specified in [strings.access]/2 to return a reference to a CharT() (aka a null character) for operator[](size()) without any conditions.

The strange wording has also been replaced via editorial change in 2018, see https://github.com/cplusplus/draft/pull/1879.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文