可以用“and”、“or”来表示吗?等等,而不是“&&”,“||”?
我习惯了 C++ 中的 and
和 or
关键字。我一直在使用它们,并且打字对我来说既快速又舒适。我听说这些别名是非标准的,可能不适用于所有编译器。但我不确定,我真的不知道这是不是真的。
假设我将代码交给某人,他在编译时会遇到问题吗?
我用and
、or
代替&&
、||
可以吗?或者这些关键字真的不标准吗?
PSI 使用 MinGW 编译器。
I'm used to the and
and or
keywords in C++. I've always used them and typing them is fast and comfortable for me. Once I've heard that these aliases are non-standard and may not work on all compilers. But I'm not sure of it, I don't really know if it's true.
Let's assume that I give someone my code, will he have problems compiling it?
Is it all right when I use and
, or
instead of &&
, ||
? Or are these keywords really non-standard?
P.S.I use the MinGW compiler.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
这些关键字是标准的,并在标准的 2.5 节中进行了描述。表 2 是这些“替代代币”的表格。你可以随心所欲地使用它们,即使如果你这样做每个人都会讨厌你。
These keywords ARE standard and are described in section 2.5 of the standard. Table 2 is a table of these "alternative tokens". You can use them all you want, even though everyone will hate you if you do.
它们是新的 c++0x 标准中的标准。最新的现代编译器应该能够识别它们,尽管我认为它们还没有义务这样做。我想,无论你的船漂浮在什么地方。
They are standard in the new c++0x standard. Up-to-date modern compilers should recognise them, although I don't believe they are obliged to yet. Whatever floats your boat, I assume.
它们是标准的 C++,但对于较旧的编译器,也可能使用 MSVC 10.0(我没有检查过),您可能必须包含一个特殊的标头,[isosomethingsomething.h]
欢呼 &呵呵,,
they're standard C++, but with older compilers and possibly also with MSVC 10.0 (i haven't checked) you may have to include a special header, [isosomethingsomething.h]
cheers & hth.,
我总是搞乱 ^(异或)和 ~(两个补码)运算符。对于替代标记(我认为应该是主要标记),它们的作用是毫无疑问的,是的,我同意以前的发帖者的观点,即文本标记更具描述性。
使用二合字母还有另一种可能的混乱,可能会忘记 ||、&& 中的字符之一。这会导致微妙的错误和奇怪的行为。
使用文本运算符,就更难犯这样的错误。
我相信我上面提到的这些都是提高代码安全性和清晰度的真正有效的论据。在我看来,大多数 C++ 程序员应该尝试习惯文本运算符,转而使用旧的神秘运算符。
我很惊讶很少有程序员了解它们。在我看来,这些运营商早就应该接管了。
I have always messed up ^ (xor) and the ~ (two complement) operators. With the alternative tokens (that I believe should be primary ones) there is no question about what they do, yes, I agree with former posters that the textual ones are much more descriptive.
There is another possible messup using the digraphs, it is possible to forget one of the characters in ||, && that will cause subtle bugs and strange behaviours.
With the textual operators, it is much harder to make such a mistake.
I believe what I mentioned above are real valid arguments to improve code safety and clarity. Most C++ programmers SHOULD in my opinion try to get used to the textual operators in favor of the old cryptic ones.
I am surprised that so few programmers know about them. These operators should have taken over long time ago as I see it.
它在语法上是有效的,因为这些是必需的替代标记。
然而,某些标记不仅仅用于逻辑或按位运算符。所以人们可以看到其特质:
甚至
这会让人们摸不着头脑。
It's syntactically valid, given those are required alternative tokens.
However, some tokens are used for more than just logical or bitwise operators. So one can see the idiosyncratic:
Or even
That's gonna make one scratch their head for a while.
哇,多年来我一直在使用和查看许多 C++ 代码示例......但直到现在才知道这些,所以我想这意味着大多数人不使用它们。因此,为了保持一致性(如果您计划在小组项目中工作等),最好养成使用 && 的习惯。和||。
Wow, i've been using and looking at many C++ code examples for years.. and never, until now, knew about these so I guess that means most people don't use them. So, for the sake of consistency (if you plan on working in group projects etc) it's probably best to make a habit of using && and ||.
ISO/IEC 14882:1998 标准(原始 C++ 标准)的第 2.5 节说:
没有讨论“如果包含一些标头”(尽管在 C 中,您需要
#include
)。任何不支持关键字或二合字母的实现都不符合 1998 年版本的 C++ 标准,更不用说更高版本了。Section 2.5 of the ISO/IEC 14882:1998 standard (the original C++ standard) says:
There is no discussion of 'if you include some header' (though in C, you need
#include <iso646.h>
). Any implementation that does not support the keywords or digraphs is not compliant with the 1998 edition, let alone later editions, of the C++ standard.显然,就向后兼容性而言,“和/或”关键字不是问题。我相信它们是更新的标准。只是老程序员不明白,一些菜鸟可能必须能够阅读代码,而不想查找 && 的内容。方法。话又说回来,如果任何 IT 部门是值得的,它会让程序员符合公司的标准!这是我的信念,所以(和/或)是未来的、真正可能的标准。 &&向后兼容 not(双关语)(和/或)。
Obviously in regards to backward compatability the "and/or" keywords are not the issue. I would believe them to be the newer standard. It is just old programmers not understanding that some noob might have to be able to read the code and not want to look up what && means. Then again if any IT department is worth it's salt it will make the programmers conform to the standards of the company! That is my belief so (and/or) are futuristic and real possible standard going towards the future. && is backward compatable not(pun) (and/or).
您还可以使用“*”代替 &&和“+”而不是 ||
You can also use '*' instead of && and '+' instead of ||
事实上,它们是 C++ 中的标准,由 ISO 14882:2003 C++ 标准 2.5/2 定义(实际上,由 1998 版标准定义)。请注意,它们内置于语言本身中,不需要您包含某种头文件。
然而,它们很少被使用,而且我还没有看到实际使用替代令牌的生产代码。替代标记存在的唯一原因是因为某些键盘(尤其是非 QWERTY 键盘)上的这些字符要么不存在,要么输入起来很笨拙。它仍然在向后兼容的标准中。
尽管它们是标准的,但我强烈建议您不要使用它们。替代标记需要输入更多字符,并且 QWERTY 键盘布局已经具有键入 C++ 代码所需的所有字符,而无需使用替代标记。此外,它们很可能会让代码的读者感到困惑。
They are in fact standard in C++, as defined by the ISO 14882:2003 C++ standard 2.5/2 (and, indeed, as defined by the 1998 edition of the standard). Note that they are built into the language itself and don't require that you include a header file of some sort.
However, they are very rarely used, and I have yet to see production code that actually uses the alternative tokens. The only reason why the alternative tokens exist in the first place is because these characters on some keyboards (especially non-QWERTY ones) were either nonexistent or clumsy to type. It's still in the standard for backwards compatibility.
Even though they are standard, I highly recommend that you don't use them. The alternative tokens require more characters to type, and the QWERTY keyboard layout already has all the characters needed to type out C++ code without having to use the alternative tokens. Also, they would most likely bewilder readers of your code.