我可以取消引用std :: string.end()吗? [英] Can I dereference std::string.end()?

查看:41
本文介绍了我可以取消引用std :: string.end()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信对此的常见回答是否",因为容器的 end()迭代器表示过去"地址,这是未定义的行为,可以取消引用.即使在字符串相对于其他容器而言有特殊情况的情况下,我也无法在标准中找到明确的语句来将字符串排除在此约束之外.

I believe a common response to this is "no," as the end() iterator for containers represents a "past-the-end" address which is undefined behavior to dereference. I can't find an explicit statement in the standard that exempts strings from this constraint, even though strings have a special case over other containers.

C ++ 11标准声明您可以读取字符串末尾的一个索引. string [size()] 引用一个空终止符的只读值.

The C++11 standard declares that you can read one index past the end of a string. string[size()] references a read-only value of a null terminator.

24.3.2.5 basic_string元素访问权限[string.access]

const_reference运算符[](size_type pos)const;

引用运算符[](size_type pos);

(1)要求: pos< = size().

(2)返回: *(begin()+ pos)如果pos<size().否则,返回对一个类型的对象的引用值为 charT() charT ,其中将对象修改为除 charT()之外的任何其他值会导致未定义的行为.

(2) Returns: *(begin() + pos) if pos < size(). Otherwise, returns a reference to an object of type charT with value charT(), where modifying the object to any value other than charT() leads to undefined behavior.

front()定义为等效于 return operator [](0),等效于 return operator [](size())表示空字符串.

front() is defined to be equivalent to return operator[](0) which is equivalent to return operator[](size()) for an empty string.

end()-begin()的定义很明确,它是字符串长度的差异,因此, end()必须指向该字符串的索引 size()用于定义该算法的合理实现.

end() - begin() is well-defined to be a difference of the length of the string, so end() must be pointing to the index of size() for a sane implementation to define that arithmetic.

在上述标准摘录中,如果 pos<,则声明 operator [](pos)等效于 *(begin()+ pos).size().是不是 表示您可以取消引用 begin()+ size(),但是您认为合理定义这一定义是否合理?或者更好的是,您知道一些证明可以免除字符串迭代器约束的证据吗?

In the above standard excerpt, it states that operator[](pos) is equivalent to *(begin() + pos) if pos < size(). It does not say that you can dereference begin() + size(), but do you think it is reasonable to assume that this should be well defined? Or better yet, do you know of some proof that exempts string iterators from the constraint?

此外,是否可以证明任何 i *(begin()+ i)等同于 operator [](i)?

Additionally, can it be proven that *(begin() + i) for any i is equivalent to operator[](i)?

推荐答案

来自

返回:一个迭代器,它是结束时的值.

以及过去的定义中:

...这样的值称为过去值.为其定义表达式* i的迭代器i的值称为可取消引用.库从不假定,过去的值是可取消引用的....

... Such a value is called a past-the-end value. Values of an iterator i for which the expression *i is defined are called dereferenceable. The library never assumes that past-the-end values are dereferenceable. ...

重点是我的,我想在第一个链接中会提到对 std :: string 所做的任何异常.由于不是,因此省略 std :: string.end()的引用是不确定的.

The emphasis is mine, and I would guess that any exception made for std::string would be mentioned in the first link. Since it's not, dereferencing std::string.end() is undefined by omission.

这篇关于我可以取消引用std :: string.end()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆