問題描述
我發現自己一再被 rdstate()
標志弄糊涂了 - good()
、bad()
、eof()
、fail()
- 以及它們在 basic_ios::operator!
、operator bool
和 operator 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
表示流出現了嚴重問題.這可能是緩沖區錯誤或向流中提供數據的任何錯誤.如果設置了此標志,您很可能不會再使用該流.
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
意味著從流中提取或讀取失敗(或寫入或插入輸出流),您需要注意該失敗.
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
表示輸入流已經結束,沒有什么可讀取的了.請注意,這僅在您嘗試從已到達其末尾的輸入流中讀取后設置(即,在發生錯誤時設置,因為您嘗試讀取不存在的數據).
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).
failbit
也可以由許多到達 EOF 的操作設置.例如,如果流中只剩下空白,并且您嘗試讀取 int
,那么您將同時到達 EOF 并且無法讀取 int
,因此兩個標志都將被設置.
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.
fail()
函數測試 badbit ||失敗位
.
good()
函數測試 !(badbit || failbit || eofbit)
.也就是說,當沒有設置任何位時,流是好的.
The good()
function tests !(badbit || failbit || eofbit)
. That is, a stream is good when none of the bits are set.
您可以使用 ios::clear()
成員函數重置標志;這允許您設置任何錯誤標志;默認情況下(不帶參數),它會清除所有三個標志.
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.
流不會重載operator bool()
;operator void*()
用于實現安全布爾習語的一個有點損壞的版本.如果設置了 badbit
或 failbit
,則此運算符重載返回 null,否則返回非 null.您可以使用它來支持測試提取成功作為循環或其他控制流語句的條件的習慣用法:
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
}
operator!()
重載與 operator void*()
相反;如果設置了 badbit
或 failbit
,則返回 true
,否則返回 false
.operator!()
重載不再需要了;它可以追溯到完全一致地支持運算符重載之前(參見 sbi 的問題 "為什么 std::basic_ios 重載了一元邏輯否定運算符?").
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 修復了導致我們必須使用安全 bool 習慣用法的問題,因此在 C++0x 中,basic_ios
基類模板確實重載了 operator bool()
作為顯式轉換運算符;此運算符與當前的 operator void*()
具有相同的語義.
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 上標志的語義的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!