“&&"和“&&"之间的区别和“和"经营者 [英] Difference between "&&" and "and" operators
问题描述
我看到了替代运算符(例如and
,or
, not
等)浏览cppreference时.
I saw alternative operators (like and
, or
, not
etc.) when browsing cppreference.
它们是&&
,||
,!
等常规"运算符的替代方案.
They are alternatives to "normal" operators like &&
, ||
, !
etc.
我检查了汇编中是否使用了&&
和and
的代码.两个版本都生成相同的程序集.
I examined the assembly for code that uses &&
and and
. Both versions generated the same assembly.
代码:
#include <iostream>
int n = 1;
int main()
{
// if(n > 0 && n < 5)
if(n > 0 and n < 5)
{
std::cout << "n is small and positive\n";
}
}
所以我的问题是:
-
&&
和and
运算符有什么区别? - 我何时何地在
&&
上使用and
? - 如果没有区别,那么C ++为什么引入替代运算符(例如
and
,or
,not
等)?
- What is the difference between the
&&
andand
operators? - Where and when do I use
and
over&&
? - If there is no difference, then why does C++ introduce alternative operators (like
and
,or
,not
etc.)?
推荐答案
&&和之间的区别是什么?和和运算符?
What is the difference between the && and and operators?
没有 1 .这些运算符的替代"方面意味着,从语义的角度来看,它们可以用于构造完全相同的表达式.
There is none1. The "alternative" aspect of these operators means that they can be used to construct the exact same expressions from a semantic perspective.
我在何时何地使用&&&?
Where and when do I use and over &&?
这在很大程度上是一个优先事项.我已经习惯了&&
不使用它,但是可以理解是否有人发现and
更具可读性.
This is largely a matter of preference. I'm too used to &&
to not use it, but can understand if someone finds and
more readable.
为什么C ++引入了替代运算符?
why does C++ introduce alternative operators?
C ++被设计为可在各种字符集和平台上使用.象Bathsheba指出的那样,三部曲是这种功能的另一个例子.如果一个字符集不允许写入&&
(例如,因为它根本没有&字符),那么仍然可以使用其他表示形式.如今,这在很大程度上尚无定论.
C++ was designed to be available on a variety of character sets and platforms. Trigraphs, like Bathsheba pointed out, are another example of such a feature. If a character set would not allow &&
to be written (say, because it simply didn't have the & character) then one can still get by with the alternative representation. Nowadays, it's largely moot.
1 实际上,经过进一步的思考,我对第一个问题的回答可以得到完善.关于令牌的解析方式,稍有不足. &&
不需要将空间解析为单独的标记,而and
则需要.这意味着:
1 Actually, upon further thinking, my answer to your first question can be refined. There is a slight lack of equivalence, pertaining to how tokens are parsed. &&
doesn't require a space to be parsed as a separate token, while and
does. That means:
void foo(bool b1, bool b2) {
if(b1&&b2) { // Well formed
}
if(b1andb2) { // ill formed, needs spaces around `and`
}
}
这篇关于“&&"和“&&"之间的区别和“和"经营者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!