C++ 这是什么=!操作员?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21029174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's this =! operator?
提问by m0nhawk
I was surprised by this code:
我对这段代码感到惊讶:
if (a =! b) { // let it be on false
...
}
But a
is never assigned by a value. What's this operator about?
但a
永远不会被赋值。这个运营商是干什么的?
回答by Mike Seymour
That's two operators, =
and !
, not one. It might be an obfuscated way of writing
那是两个运算符,=
而!
,不是一个。这可能是一种混淆的写作方式
a = !b;
if (a) {
// whatever
}
setting a
to the logical inverse of b
, and testing whether the result is true (or, equivalently, whether b
was false).
设置a
为 的逻辑逆b
,并测试结果是否为真(或等效地,是否b
为假)。
Or it might be a mistyping of a != b
.
或者它可能是a != b
.
回答by Wexxor
Long ago, when dinosaurs roamed the earth and C ran on 5th edition UNIX on PDP-11s, =!
was the 'not equals' operator. This usage was deprecated by the creation of Standard C, so now it means 'assign the logical inverse', as in a = !b
. This is a good argument for always surrounding binary operators with spaces, just to make it clear to the humansreading the code what the compiler is thinking.
很久以前,当恐龙在地球上漫游并且 C 在 PDP-11 上的第 5 版 UNIX 上运行时,C=!
是“不等于”运算符。这种用法已被标准 C的创建弃用,所以现在它的意思是“分配逻辑逆”,如a = !b
. 这是一个很好的论据,总是用空格包围二元运算符,只是为了让阅读代码的人清楚编译器在想什么。
I'm a bit surprised nobody else mentioned this, but then again I may be the only SO user to have ever touched a C compiler that old.
我有点惊讶没有其他人提到这一点,但我可能是唯一一个接触过这么老的 C 编译器的 SO 用户。
回答by Timbo
a
is assigned the boolean negation of b
in that line. It is just a misformatted
a
b
在该行中分配了布尔否定。这只是格式错误
if( a = !b ) {
... and an evil hidden assignment inside a condition.
...以及条件中的邪恶隐藏任务。
回答by splrs
a =! b
is just a funny way of putting
只是一种有趣的放置方式
a = !b
i.e. the assignment of not b
to a
.
即分配not b
到a
。
The value of the expression is a
after the assignment.
表达式的值a
在赋值之后。
With the code below you can see that the value of the expression a = !b
is !false
(i.e. true
), and you can then see the assignment has taken place by checking the value of a
, which is also true
.
使用下面的代码,您可以看到表达式的a = !b
值为!false
(ie true
),然后您可以通过检查 的值来查看赋值,该值a
也是true
。
#include <iostream>
int main()
{
bool a = false;
bool b = false;
if(a)
printf("a is true!\n");
else
printf("a is false!\n");
if(a = !b)
printf("expression is true!\n");
else
printf("expression is false!\n");
if(a)
printf("a is true!\n");
else
printf("a is false!\n");
}
Result:
结果:
a is false!
expression is true!
a is true!
回答by Simpal Kumar
According to C/C++ operators list there is no operator such as =!
. However, there is an operator !=
(Not equal to, Comparison operators/relational operator)
根据 C/C++ 运算符列表,没有诸如=!
. 但是,有一个运算符!=
(不等于,比较运算符/关系运算符)
There are two possibilities.
有两种可能。
- It couldbe typo mistake as I've noticed that
=!
operators is inif
statement and someone is trying to type!=
instead of=!
because!=
is the comparison operator which returns true or false. - Possibly, the developer was trying to assign the boolean negation of
b
toa
and he/she has done a typo mistake and forgot to put a space after equal sign. This is how the compiler interprets it, anyways. According to Operator precedence in c++:- Operator Logical NOT (
!
) precedenceis 3and Associativityis Right-to-left - Operator Direct assignment (=) precedenceis 16and Associativityis Right-to-left
- Operator Logical NOT (
- 这可能是打字错误,因为我注意到
=!
运算符在if
语句中,有人试图输入!=
而不是=!
因为!=
比较运算符返回true 或 false。 - 也许,开发商正试图分配的布尔非
b
到a
他/她做了一个错字的错误,忘记把一个空间等号后。无论如何,这就是编译器解释它的方式。根据c++ 中的运算符优先级:- 运算符逻辑非 (
!
) 优先级为3,关联性为从右到左 - 运算符直接赋值 (=) 优先级为16,关联性为从右到左
- 运算符逻辑非 (
回答by Shoe
They are two different operators: the =
(assignment) operator together with the !
operator. It can basically be translated to an assignment of a
to the negated value of b
.
它们是两个不同的运算符:(=
赋值)运算符和!
运算符。它基本上可以转换为a
对 的否定值的赋值b
。
if (a = !b)
But, what the user, probably, meant to write was the !=
operator:
但是,用户可能想要写的是!=
操作员:
if (a != b)
回答by Andon M. Coleman
That is not a single operator, it is however, a great way to obfuscate code.
这不是一个单一的运算符,但是,它是一种混淆代码的好方法。
If it were written a=!b
instead, the white space might not lead you to believe that it was a single operator.
如果a=!b
改为写入,空格可能不会让您相信它是单个运算符。
Compilers have warnings for assignment in a conditional expression unless you wrap the entire statement in a set of parenthesis, and this is a perfect example of when this warning would be useful.
除非您将整个语句括在一组括号中,否则编译器会对条件表达式中的赋值发出警告,这是此警告何时有用的完美示例。
Both of these statements are functionally identical, but one generates a warning and the other does not:
这两个语句在功能上是相同的,但一个生成警告,另一个不生成:
if (a =! b) // Generates a warning with `-Wparentheses` (gcc)
if ((a =! b)) // No such warning
-Wparentheses
Warn if parentheses are omitted in certain contexts, such as when there is an assignment in a context where a truth value is expected, or when operators are nested whose precedence people often get confused about.
- 括号
在某些上下文中省略括号时发出警告,例如当在需要真值的上下文中进行赋值时,或者当运算符嵌套时,人们经常对其优先级感到困惑。
This, of course, assumes that you are a responsible programmer and actually read the warnings your compiler spits out.
当然,这假设您是一个负责任的程序员并实际阅读了编译器发出的警告。
Using white space in a more reasonable way, the statement is actually:
以更合理的方式使用空格,语句实际上是:
if (a = !b) // Assign A the value of (logical) NOT B and then test the truth
The compiler warning mentioned above is actually useful in the case where the person who wrote this code accidentally transposed !
and =
. However, from the cryptic inline comments in your original question, a = !b
is probably the author's intention.
上面提到的编译器警告是在谁写了这个代码的人无意中调换的情况下,真正有用的!
和=
。但是,从您原始问题中神秘的内联评论来看,a = !b
可能是作者的意图。
回答by AnT
C++ does not have an =!
operator, which immediately means that this is an =
operator followed by a !
operator. So, you simply have a = !b
in the if
condition.
C++ 没有=!
运算符,这立即意味着这是一个=
运算符后跟一个!
运算符。所以,你只是a = !b
在if
条件。
回答by Ryman Holmes
This is all about clarity of code:
这完全是关于代码的清晰度:
It should be written as: if (a = !b)
应该写成: if (a = !b)
if (a = !b)
is the same as saying if a
is assigned to !b
. So there are technically 2 separate operators being used, =
which is an assignment operation, and !
which is a logical expression.
if (a = !b)
与说 if a
分配给!b
. 所以从技术上讲,使用了 2 个单独的运算符,=
这是一个赋值操作,!
这是一个逻辑表达式。
Just put a spacebetween =
and !
it solves this confusion.
只要把一个空间之间=
,并!
解决了这种混乱。
回答by Jojodmo
There could be three reasons for this:
造成这种情况的原因可能有以下三个:
It could be a mistyping of the
!=
operator, meaning not equal to. Example:if (a != b) { // a is not equal to b }
It could be a mistyping
a == !b
, meaninga
is equal to notb
, which would most commonly be used with booleans. Example:if (a == !b) { // The boolean a is equal to not b (a is not equal to b) }
It could be trying to assign
a
to the inverse ofb
. Example:bool a = !b; // Sets a to the opposite of b
可能是
!=
操作符输入错误,意思是不等于。例子:if (a != b) { // a is not equal to b }
它可能是一个错误输入
a == !b
,意思a
是等于 notb
,它最常用于布尔值。例子:if (a == !b) { // The boolean a is equal to not b (a is not equal to b) }
它可能试图分配
a
给 的倒数b
。例子:bool a = !b; // Sets a to the opposite of b