Eva*_*ran 8 c++ string standards defects
这个问题没有与之相关的实际问题,更多的是好奇心,想要知道我是不是太过于字面意思;).
所以我一直在努力尽可能多地理解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.
Run Code Online (Sandbox Code Playgroud)
对我来说似乎很理智.但后来我心想,等一下是什么定义data()?
const charT* data() const;
Run Code Online (Sandbox Code Playgroud)
是的,它返回一个.const charT*
很明显,非const版本operator[]不能简单地实现,return data()[pos]因为那将是char&从类型的表达式初始化类型的引用const char.
我认为这是很明显,目的是data()要实现像return data_;和operator[]被实现为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.
Run Code Online (Sandbox Code Playgroud)
所以也许我偶然发现了一些已经讨论过的东西.
| 归档时间: |
|
| 查看次数: |
258 次 |
| 最近记录: |