問題描述
我發(fā)現(xiàn)自己一再被 rdstate()
標(biāo)志弄糊涂了 - good()
、bad()
、eof()
、fail()
- 以及它們?cè)?basic_ios::operator!
、operator bool
和 operator void 中的表達(dá)方式*
.
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*
.
有人可以讓我擺脫痛苦并解釋這一點(diǎn),這樣我就不必再三思了嗎?
Could somebody put me out of my misery and explain this so I never have to think twice again?
推薦答案
有三個(gè)標(biāo)志指示錯(cuò)誤狀態(tài):
There are three flags that indicate error state:
badbit
表示流出現(xiàn)了嚴(yán)重問題.這可能是緩沖區(qū)錯(cuò)誤或向流中提供數(shù)據(jù)的任何錯(cuò)誤.如果設(shè)置了此標(biāo)志,您很可能不會(huì)再使用該流.
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
意味著從流中提取或讀取失敗(或?qū)懭牖虿迦胼敵隽?,您需要注意該失敗.
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
表示輸入流已經(jīng)結(jié)束,沒有什么可讀取的了.請(qǐng)注意,這僅在您嘗試從已到達(dá)其末尾的輸入流中讀取后設(shè)置(即,在發(fā)生錯(cuò)誤時(shí)設(shè)置,因?yàn)槟鷩L試讀取不存在的數(shù)據(jù)).
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
也可以由許多到達(dá) EOF 的操作設(shè)置.例如,如果流中只剩下空白,并且您嘗試讀取 int
,那么您將同時(shí)到達(dá) EOF 并且無法讀取 int
,因此兩個(gè)標(biāo)志都將被設(shè)置.
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()
函數(shù)測(cè)試 badbit ||失敗位
.
good()
函數(shù)測(cè)試 !(badbit || failbit || eofbit)
.也就是說,當(dāng)沒有設(shè)置任何位時(shí),流是好的.
The good()
function tests !(badbit || failbit || eofbit)
. That is, a stream is good when none of the bits are set.
您可以使用 ios::clear()
成員函數(shù)重置標(biāo)志;這允許您設(shè)置任何錯(cuò)誤標(biāo)志;默認(rèn)情況下(不帶參數(shù)),它會(huì)清除所有三個(gè)標(biāo)志.
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.
流不會(huì)重載operator bool()
;operator void*()
用于實(shí)現(xiàn)安全布爾習(xí)語的一個(gè)有點(diǎn)損壞的版本.如果設(shè)置了 badbit
或 failbit
,則此運(yùn)算符重載返回 null,否則返回非 null.您可以使用它來支持測(cè)試提取成功作為循環(huán)或其他控制流語句的條件的習(xí)慣用法:
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*()
相反;如果設(shè)置了 badbit
或 failbit
,則返回 true
,否則返回 false
.operator!()
重載不再需要了;它可以追溯到完全一致地支持運(yùn)算符重載之前(參見 sbi 的問題 "為什么 std::basic_ios 重載了一元邏輯否定運(yùn)算符?").
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 修復(fù)了導(dǎo)致我們必須使用安全 bool 習(xí)慣用法的問題,因此在 C++0x 中,basic_ios
基類模板確實(shí)重載了 operator bool()
作為顯式轉(zhuǎn)換運(yùn)算符;此運(yùn)算符與當(dāng)前的 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*()
.
這篇關(guān)于basic_ios 上標(biāo)志的語義的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!