是否存在非短路逻辑“和”在C ++? [英] Is there an Non-Short circuited logical "and" in C++?

查看:190
本文介绍了是否存在非短路逻辑“和”在C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tl; dr:在C ++中是否有非短路逻辑AND(类似于&&)?

tl;dr: Is there a non-short circuit logical AND in C++ (similar to &&)?

我有两个函数想要调用,并使用返回值来计算出第三个复合函数的返回值。问题是,我总是希望两个函数都能评估(因为它们输出系统状态的日志信息)。

I've got 2 functions that I want to call, and use the return values to figure out the return value of a 3rd composite function. The issue is that I always want both functions to evaluate (as they output log information about the state of the system)

IE:

bool Func1(int x, int y){
  if( x > y){
    cout << "ERROR- X > Y" << endl;
  }
}
bool Func2(int z, int q){
  if( q * 3 < z){
    cout << "ERROR- Q < Z/3" << endl;
  }
}
bool Func3(int x, int y, int z, int q){
  return ( Func1(x, y) && Func2(z, q) );
}

当然,条件函数在函数中并不那么简单,是的,我意识到,我可以使用临时变量来存储两个函数的返回,然后对临时变量执行短路逻辑,但我想知道是否有一个优雅的语言解决方案来保持一个在Func3中的行返回,同时仍然从两个函数获取日志消息。

Of course, the conditionals aren't quite that simple in the functions, and yes, I realize that I could use temporary variables to store the returns of the two functions and then do the "short-circuit" logic on the temporary variables, but I was wondering if there was an "elegant" language solution to keep the one-line return in Func3 while still getting the logging messages from both functions.

回应摘要:

|和&可以用来得到效果,但只有当返回类型是bool。我发现在ANSI C ++规范中没有提到这一点。从我可以告诉,这是因为bool被转换为一个int(true = 1,false = 0),然后使用位运算符,然后它被转换回bool。

The "bitwise" operators | and & can be used to get the effect, but only if the return type is bool. I found no mention of this in the ANSI C++ spec. From what I can tell, this works because the "bool" is converted to an int (true = 1, false = 0), and then the bitwise operator is used, then it is converted back to a bool.

也可以使用运算符 + * 。这在ANSI C ++规范中没有提及,但是可能工作,因为与上述相同的原因。 + 给出或,因为true被转换为1,然后除0之外的任何转换回true。 * 适用于和因为1(真)* 0(假)== 0(假)和1(真)* 1 (true)

The Operators "+" and "*" can also be used. This is not mentioned in the ANSI C++ Spec, but probably works because of the same reason as above. "+" give "or" because true is converted to 1, and then anything other than 0 is converted back to true. "*" works for "and" because 1 (true) * 0 (false) == 0(false) and 1(true) * 1(true) == 1(true)

这两个都似乎依赖于隐式类型转换为整数,然后回到bool。

Both of these seem to rely on implicit type conversion to integer and then back to bool. Both of these will likely mess up whomever tries to maintain the code.

其他回答归结为只使用临时或实现您自己的,这不是问题。目标是看看是否已经有一个在C ++标准中实现的操作符。

Other responses boil down to "Just use temporaries" or "Implement your own" which was not the question. The goal was to see if there was already an operator implemented in the C++ standard to do it.

推荐答案

& 操作符对 bool 操作数执行逻辑和操作,并且不短路。

The & operator performs logical "and" operation for bool operands and is not short circuited.

这不是一个序列点。你不能依赖操作数的求值顺序。

It's not a sequence point. You cannot rely on the order of evaluation of the operands. However, it's guaranteed that both operands are evaluated.

建议这样做。使用临时变量是一个更好的解决方案。不要为聪明的代码牺牲可读性。

I do not recommend doing this. Using temporary variables is a better solution. Don't sacrifice readability for "clever code".

这篇关于是否存在非短路逻辑“和”在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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