[英]Is calling operator>> on a istream in a fail/bad state undefined behavior?
int main()
{
std::ifstream istr( "foo.txt" );
int a, b;
istr >> a;
istr >> b;
}
Suppose the line istr >> a
sets the stream's failbit or errorbit. 假设行
istr >> a
设置流的failbit或errorbit。 Is it defined behavior to subsequently call istr >> b
? 是否定义了随后调用
istr >> b
行为?
Yes, that's well-defined. 是的,这是明确定义的。 The first stage of formatted input is to construct a
sentry
object, which checks the state of the stream. 格式化输入的第一阶段是构造一个检测流的状态的
sentry
对象。 If the state is not good, then the extraction does nothing. 如果状态不好,那么提取什么都不做。
It would be undefined behaviour to use the value of b
afterwards, since it would still be uninitialised. 之后使用
b
的值将是未定义的行为,因为它仍然是未初始化的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.