简体   繁体   English

在故障/坏状态未定义行为的istream上调用operator >>吗?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM