【发布时间】:2013-11-02 23:07:22
【问题描述】:
我对如何/为什么可以在条件句中使用 istream 的理解很脆弱。我读过这个问题:(Why istream object can be used as a bool expression?)。
我不明白为什么编译没有错误......
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
}
【问题讨论】:
-
这是哪个编译器?
-
libc++,我想。 (Xcode 和/或 CodeRunner)
-
嗯,我主要担心的是它显示
int而不是bool。无论如何,应该有一个到bool的显式转换运算符,但我认为还没有任何实现。 -
是否在 c++11 模式下?
operator bool仅在 c++11 中添加。 -
是的,c++11 模式。 (我也对
int感到困惑)