我對如何/爲什麼可以在條件中使用istream有一個脆弱的理解。我讀過這個問題:(Why istream object can be used as a bool expression?)。這個比較爲什麼不bool爲istream工作?
我不明白爲什麼這個編譯沒有錯誤...
while (cin >> n) {
// things and stuff
}
......而這無法編譯(消息error: invalid operands to binary expression ('int' and '__istream_type' (aka 'basic_istream<char, std::char_traits<char> >'))
)
while (true == (cin >> n)) {
// things and stuff
}
這是哪個編譯器? – chris
libC++,我想。 (Xcode和/或CodeRunner) – griotspeak
那麼,我主要關心的是它說'int'而不是'bool'。無論如何,應該有一個明確的轉換運算符來「布爾」,但我認爲任何實現都沒有。 – chris