标准中的小(不重要)缺陷?

发布于 2024-09-08 06:21:58 字数 1325 浏览 5 评论 0原文

这个问题没有与之相关的实际问题,更多的是出于好奇,想知道我是否太从字面上理解了;)。

因此,我一直在努力尽可能多地理解 C++ 标准。今天,在深入研究该标准时,我注意到这一点(ISO/IEC 14882:2003 21.3.4):

const_reference operator[](size_type pos) const;
reference operator[](size_type pos);
Returns: If pos < size(), returns data()[pos].
         Otherwise, if pos == size(), the const version returns charT().
         Otherwise, the behavior is undefined.

对我来说似乎相当理智。但后来我心想,等一下 data() 的定义是什么?

const charT* data() const;

是的,它返回一个 const charT*

显然,operator[] 的非常量版本不能实现为简单的 return data()[pos],因为这将初始化 类型的引用char& 来自 const char 类型的表达式。

我认为很明显,意图data() 实现类似 return data_;operator[]< /code> 被实现为 return data_[pos]; 或功能类似的东西,但这不是标准所说的:-P。

如果我没记错的话,实施者有一定的余地,他们可以随心所欲地实施事情,只要满足给定的基本要求并具有相同的净效果即可。

所以问题是,我是否太字面意思了,或者这是否会被视为缺陷。

编辑:值得注意的是,c++0x 草案已将措辞更改为:

Returns: If pos < size(), returns *(begin() + pos).
         Otherwise, if pos == size(), the const version returns charT().
         Otherwise, the behavior is undefined.

所以也许我只是偶然发现了已经讨论过的内容。

This question has no practical issues associated with it, it is more a matter of curiosity and wanting to know if I am taking things too literally ;).

So I have been trying to work towards understanding as much of the c++ standard as possible. Today in my delving into the standard I noticed this (ISO/IEC 14882:2003 21.3.4):

const_reference operator[](size_type pos) const;
reference operator[](size_type pos);
Returns: If pos < size(), returns data()[pos].
         Otherwise, if pos == size(), the const version returns charT().
         Otherwise, the behavior is undefined.

Seems pretty sane to me. But then I thought to myself, wait a sec what's the definition of data()?.

const charT* data() const;

yup, it returns a const charT*.

Clearly the non-const version of operator[] cannot be implemented as a simple return data()[pos] then since that would be initializing a reference of type char& from an expression of type const char.

I think that it is obvious that the intent is that data() be implemented something like return data_; and operator[] be implemented as return data_[pos]; or something functionally similar, but that's not what the standard says :-P.

If I recall correctly, implementors have some leeway in that they can implement things how they please as long as it meets the basic requirements given and has the same net effect.

So the question is, am I being way too literal, or is this the type of thing that would be considered a defect.

EDIT: It is worth noting that the c++0x draft has changed the wording to:

Returns: If pos < size(), returns *(begin() + pos).
         Otherwise, if pos == size(), the const version returns charT().
         Otherwise, the behavior is undefined.

So perhaps I have just stumbled onto something that has already been discussed.

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

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

发布评论

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

评论(2

久而酒知 2024-09-15 06:21:58

是的,这是一个缺陷,是的,这就是修复。

http://www.open- std.org/JTC1/SC22/WG21/docs/lwg-defects.html#259

Yes, it was a defect and yes, this was the fix.

http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-defects.html#259

紫轩蝶泪 2024-09-15 06:21:58

我假设他们在定义中使用 data() 而不是 data_ 因为他们想严格按照公共接口进行定义。

I assume that they used data() in the definition instead of data_ becuase they wanted to define in strictly in terms of the public interface.

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