标准中的小(不重要)缺陷?
这个问题没有与之相关的实际问题,更多的是出于好奇,想知道我是否太从字面上理解了;)。
因此,我一直在努力尽可能多地理解 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是一个缺陷,是的,这就是修复。
Yes, it was a defect and yes, this was the fix.
我假设他们在定义中使用
data()
而不是data_
因为他们想严格按照公共接口进行定义。I assume that they used
data()
in the definition instead ofdata_
becuase they wanted to define in strictly in terms of the public interface.