c++ - What does cin >> x evaluate to, on error?
问题描述
Consider this snippet:
int a;
while (cin >> a){/* do something */}
On running this code, suppose I enter a string. The loop is exited. But then, since the expression inside while
ends in error, and it doesn't evaluate to a bool
type (is this incorrect thinking?), how does the compiler know to exit the loop?
解决方案
cin >> x
returns cin
to allow chaining.
And in an boolean context, cin
evaluates to true
if and only if the last operation was successful.
Long story short, the loop will end on the first end of file or error.
推荐阅读
- python - 如何根据输入定义两种迭代方式
- unity3d - 更改应用程序图像/外观
- php - login.php 错误未在浏览器上加载
- c++ - 即使使用清单文件,IsWindows10OrGreater 也总是返回 False
- react-native - 如何通过样式更改处理按下和取消按下的可触摸组件?
- haskell - 实例声明中的副作用
- security - Asterisk 上的 SRTP
- python - 我们如何在 Airflow 中导入 dag?
- docker - 在 dockerd 参数中设置 -H tcp://XXXX:2375 后无法连接到 Docker 守护进程
- pipe - xargs: tar: 由信号 13 终止