为什么不可以?布尔值有 &&= 或 ||= 吗?
是否有可能发生“非常糟糕的事情”&&=
并且||=
被用作 bool foo = foo && 的语法糖。 bar
和 bool foo = foo ||酒吧?
Is there a "very bad thing" that can happen &&=
and ||=
were used as syntactic sugar for bool foo = foo && bar
and bool foo = foo || bar
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 C++ 中,
bool
只能为true
或false
。因此,使用&=
和|=
相对安全(尽管我不太喜欢这种表示法)。确实,它们将执行位操作而不是逻辑操作(因此它们不会短路),但这些位操作遵循明确定义的映射,这实际上相当于逻辑操作,只要因为两个操作数都是bool
类型。1与其他人在这里所说的相反,
bool
在C++ 决不能有不同的值,例如2
。当将该值分配给bool
时,它将根据标准转换为true
。将无效值放入
bool
的唯一方法是在指针上使用reinterpret_cast
:但是由于此代码无论如何都会导致未定义的行为,因此我们可以安全地忽略此潜在问题符合 C++ 代码。
1 诚然,这是一个相当大的警告,正如 Angew 的评论所示:
原因是
b & 2
执行整数提升,使得表达式等效于static_cast(b) & 2
,结果为0
,然后将其转换回bool
。因此,运算符 &&= 的存在确实会提高类型安全性。A
bool
may only betrue
orfalse
in C++. As such, using&=
and|=
is relatively safe (even though I don’t particularly like the notation). True, they will perform bit operations rather than logical operations (and thus they won’t short-circuit) but these bit operations follow a well-defined mapping, which is effectively equivalent to the logical operations, as long as both operands are of typebool
.1Contrary to what other people have said here, a
bool
in C++ must never have a different value such as2
. When assigning that value to abool
, it will be converted totrue
as per the standard.The only way to get an invalid value into a
bool
is by usingreinterpret_cast
on pointers:But since this code results in undefined behaviour anyway, we may safely ignore this potential problem in conforming C++ code.
1 Admittedly this is a rather big caveat as Angew’s comment illustrates:
The reason is that
b & 2
performs integer promotion such that the expression is then equivalent tostatic_cast<int>(b) & 2
, which results in0
, which is then converted back into abool
. So it’s true that the existence of anoperator &&=
would improve type safety.&&
和&
具有不同的语义:如果第一个操作数是,
。即类似的东西&&
将不会计算第二个操作数假是安全的,但
不是,尽管两个操作数都是
bool
类型。&=
和|=
也是如此:其行为与以下内容不同:
&&
and&
have different semantics:&&
will not evaluate the second operand if the first operand isfalse
. i.e. something likeis safe, but
is not, although both operands are of type
bool
.The same is true for
&=
and|=
:will behave differently than:
简短回答
所有运算符
+=
、-=
、*=
、/=
、& =
、|=
... 是算术并提供相同的期望:但是,运算符
&&=
和||=
> 是符合逻辑的,并且这些运算符可能容易出错,因为许多开发人员希望始终在x &&= foo()
中调用foo()
。我们真的需要让 C/C++ 变得更加复杂才能获得
x = x && 的快捷方式吗? foo()
?我们真的想进一步混淆神秘的语句
x = x && foo()
?或者我们是否想编写像
if (x) x = foo();
这样有意义的代码?长答案
&&=
的示例如果
&&=
运算符可用,则此代码:相当于:
第一个代码是 错误 -容易发生,因为许多开发人员会认为无论
f1()
返回值是什么,f2()
总是被调用。这就像写bool ok = f1() && f2();
其中,仅当f1()
返回true
时才会调用f2()
。f1()
返回true
时才调用f2()
,因此上面的第二个代码不太容易出错。f2()
),&=
就足够了:&=
的示例此外,它是编译器优化上面的代码比下面的代码更容易:
比较
&&
和&
我们可能想知道运算符
&& 和
&
在应用于bool
值时会给出相同的结果吗?让我们使用以下 C++ 代码进行检查:
输出:
结论
因此 YES 我们可以将
&&
替换为&
来表示bool< /code> 值 ;-)
因此最好使用
&=
而不是&&=
。我们可以认为
&&=
对于布尔值来说是无用的。与
||=
相同如果开发人员希望调用
f2()
, 仅当f1()
返回false
时,而不是:我建议使用以下更容易理解的替代方案:
或者如果您更喜欢全部在一行样式:
Short answer
All the operators
+=
,-=
,*=
,/=
,&=
,|=
... are arithmetic and provide same expectation:However, operators
&&=
and||=
would be logical, and these operators might be error-prone because many developers would expectfoo()
be always called inx &&= foo()
.Do we really need to make C/C++ even more complex to get a shortcut for
x = x && foo()
?Do we really want to obfuscate more the cryptic statement
x = x && foo()
?Or do we want to write meaningful code like
if (x) x = foo();
?Long answer
Example for
&&=
If
&&=
operator was available, then this code:is equivalent to:
This first code is error-prone because many developers would think
f2()
is always called whatever thef1()
returned value. It is like writingbool ok = f1() && f2();
wheref2()
is called only whenf1()
returnstrue
.f2()
to be called only whenf1()
returnstrue
, therefore the second code above is less error-prone.f2()
to be always called),&=
is sufficient:Example for
&=
Moreover, it is easier for compiler to optimize this above code than that below one:
Compare
&&
and&
We may wonder whether the operators
&&
and&
give the same result when applied onbool
values?Let's check using the following C++ code:
Output:
Conclusion
Therefore YES we can replace
&&
by&
forbool
values ;-)So better use
&=
instead of&&=
.We can consider
&&=
as useless for booleans.Same for
||=
If a developer wants
f2()
be called only whenf1()
returnsfalse
, instead of:I advice the following more understandable alternative:
or if you prefer all in one line style:
短路很重要,而且语义也很简单。 A &&= B 是 A = A && 的语法糖。 B.
单子编程的概念在 K&R 早期并不普遍,所以最好的解释似乎是 K&R 认为它并不重要,就像 QWERTY 键盘一样,我们一直忍受着它自从。
short circuiting is important, and the semantics are straightforward. A &&= B would be syntactic sugar for A = A && B.
The concept of monadic programming was not widespread in the early days of K&R, so it seems the best explanation is K&R didn't think it was important, and like the QWERTY keyboard, we've lived with it ever since.
我很惊讶没有人提到它:利用
bool
类型的隐式整数转换允许您伪造&&=
和||=< /code> 分别为
*=
和+=
。因此,以下内容按预期工作(使用
*=
而不是&&=
)并且非常有用:I'm surprised that nobody mentioned it: taking advantage of implicit integer conversion of the type
bool
allows you to fake&&=
and||=
with*=
and+=
, respectively.So, the following works as expected (using
*=
instead of&&=
) and can be quite useful: