basic_ios上的标志语义 [英] Semantics of flags on basic_ios

查看:137
本文介绍了basic_ios上的标志语义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己经常被 rdstate() flags - good() bad() eof() fail() basic_ios :: operator!运算符bool 运算符void *



有人可能让我失去我的痛苦,解释这一点,所以我再也不用再三思考了。

解决方案

有三个标志表示错误状态:




  • badbit 意味着流已经非常错误。它可能是一个缓冲区错误或任何向流中馈送数据的错误。


  • failbit 意味着从流提取或读取失败(或输出流的写入或插入),并且您需要注意该失败。


  • eofbit 表示输入流已到达结尾,没有任何内容可供阅读。请注意,只有在您尝试从已到达其结尾的输入流(即,当发生错误时设置,因为您尝试读取不在那里的数据)时,该设置才会设置。




failbit 到达EOF。例如,如果在流中只剩下空格,并且您尝试读取 int ,则您都将到达EOF,并且您将无法读取<$ c $ c> int ,因此将设置这两个标志。



<函数测试 badbit || failbit 。



good() function tests ! (badbit || failbit || eofbit)



您可以使用 ios :: clear()来重置标记, 成员函数;这允许您设置任何错误标志;默认情况下(没有参数),它清除所有三个标志。



流不会重载 operator bool() ; operator void *()用于实现一个有点破坏的安全bool成语版本。如果设置了 badbit failbit ,则此运算符过载返回null,否则为非空。您可以使用它来支持测试抽取成功的习语作为循环或其他控制流程语句的条件:

  if(std :: cin>> x){
//提取成功
}
else {
//提取失败
}

operator!() code> operator void *();如果设置了 badbit failbit ,则返回 true false operator!()重载不是真的需要的;它可以追溯到操作负载完全和一致地被支持之前(参见sbi的问题为什么std :: basic_ios重载一元逻辑否定运算符?)。



C ++ 0x修复了导致我们必须使用安全的bool成语,所以在C ++ 0x中 basic_ios 基类模板做的重载 operator bool()作为显式转换运算符;此运算符具有与当前运算符void *()相同的语义。


I find myself repeatedly baffled by the rdstate() flags - good(), bad(), eof(), fail() - and how they are expressed in basic_ios::operator!, operator bool and operator void*.

Could somebody put me out of my misery and explain this so I never have to think twice again?

解决方案

There are three flags that indicate error state:

  • badbit means something has gone very wrong with the stream. It might be a buffer error or an error in whatever is feeding data to the stream. If this flag is set, it's likely that you aren't going to be using the stream anymore.

  • failbit means that an extraction or a read from the stream failed (or a write or insertion for output streams) and you need to be aware of that failure.

  • eofbit means the input stream has reached its end and there is nothing left to read. Note that this is set only after you attempt to read from an input stream that has reached its end (that is, it is set when an error occurs because you try to read data that isn't there).

The failbit may also be set by many operations that reach EOF. For example, if there is only whitespace left remaining in the stream and you try to read an int, you will both reach EOF and you will fail to read the int, so both flags will be set.

The fail() function tests badbit || failbit.

The good() function tests !(badbit || failbit || eofbit). That is, a stream is good when none of the bits are set.

You can reset the flags by using the ios::clear() member function; this allows you to set any of the error flags; by default (with no argument), it clears all three flags.

Streams do not overload operator bool(); operator void*() is used to implement a somewhat broken version of the safe bool idiom. This operator overload returns null if badbit or failbit is set, and non-null otherwise. You can use this to support the idiom of testing the success of an extraction as the condition of a loop or other control flow statement:

if (std::cin >> x) {
    // extraction succeeded
}
else {
    // extraction failed
}

The operator!() overload is the opposite of the operator void*(); it returns true if the badbit or failbit is set and false otherwise. The operator!() overload is not really needed anymore; it dates back to before operator overloads were supported completely and consistently (see sbi's question "Why does std::basic_ios overload the unary logical negation operator?").

C++0x fixes the problem that causes us to have to use the safe bool idiom, so in C++0x the basic_ios base class template does overload operator bool() as an explicit conversion operator; this operator has the same semantics as the current operator void*().

这篇关于basic_ios上的标志语义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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