OR运算符不会同时评估这两个语句,只有1个 [英] OR operator doesn't evaluate both statements, only 1

查看:199
本文介绍了OR运算符不会同时评估这两个语句,只有1个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std::ifstream file_1(argv[1]);
std::ifstream file_2(argv[2]);
...
std::string dummy_1;
std::string dummy_2;
...
while(std::getline(file_1, dummy_1) || std::getline(file_2, dummy_2)) // the problem
{
  ...
}

条件的行为类似于OR运算符在右边评估选项左边的文件到达结束(返回FALSE),它不会同时评估它们。

The condition behaves like the OR operator is evaluating the option on the right only if the file on the left reach the end (returning a FALSE) and it doesn't evaluate them at the same time.

我知道语句的求值没有与操作符本身或与它自己的优先级规则,但我的程序不应该工作,因为我的成像​​与两个语句同时评估。

I know that the evaluation of a statement has nothing to do with the operator itself or with its own precedence rules, but my program it's not supposed to work like I'm imaging with both statements being evaluated at the same time ?

推荐答案

OR运算符是一个短路运算符,意思是如果一边计算为true,则另一边永远不会被执行。如果你的意图是双方的OR评估,那么你可能想重新考虑你的代码是如何写的。

The OR operator is a short circuit operator, meaning that if one side evaluates to true the other side is never actually executed. If your intention is to have both sides of the OR evaluated then you might want to reconsider how your code is written.

这篇关于OR运算符不会同时评估这两个语句,只有1个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆