这些陈述是否符合标准?
std::string str{"123"};
auto it = str.begin();
--it;
++it; // Does *it point to character '1' now?
Run Code Online (Sandbox Code Playgroud)
我在g ++ 4.7.2和clang ++ 3.5上尝试了这个 - *it返回'1'.这是c ++ 11中的标准行为吗?
Dan*_*rey 13
不,它无效.
它是未定义的行为,因为24.2.6 [bidirectional.iterators]声明后置条件--it是结果必须是可解除引用的.如前所述,begin()在您的示例中,不满足此条件,因此代码是非法的.
由于没有诊断需要它可能看起来工作,但你不能(也不应该)依赖于它.