逻辑运算符的书面版本

发布于 2024-08-23 13:10:31 字数 437 浏览 9 评论 0原文

这是我见过的唯一地方ornot 被列为 C++ 中的实际运算符。当我在 NetBeans 中编写测试程序时,我得到了红色下划线,好像存在语法错误,并认为该网站是错误的,但 NetBeans 是错误的,因为它按预期编译和运行。

我可以看到 !not 更受青睐,但 and && 的可读性更好。 or 似乎比它们的语法兄弟更伟大。为什么存在这些版本的逻辑运算符以及为什么似乎没有人使用它?这是真正有效的 C++ 还是该语言中包含的与 C 的某种兼容性?

This is the only place I've ever seen and, or and not listed as actual operators in C++. When I wrote up a test program in NetBeans, I got the red underlining as if there was a syntax error and figured the website was wrong, but it is NetBeans which is wrong because it compiled and ran as expected.

I can see ! being favored over not but the readability of and && or seems greater than their grammatical brothers. Why do these versions of the logical operators exist and why does seemingly no one use it? Is this truly valid C++ or some sort of compatibility with C that was included with the language?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

别念他 2024-08-30 13:10:31

它们起源于 C 头文件 。当时,有些键盘无法输入 && 所需的符号(例如),因此标头包含可以帮助它们的 #define为此,(在我们的示例中)将 and 定义为 &&。当然,随着时间的推移,这种做法已经越来越少了。

在 C++ 中,它们成为所谓的“替代标记”。您不需要需要包含任何内容来在兼容的编译器中使用这些标记(因此,C 标头的 C++ 化版本 为空)。替代标记与常规标记一样,只是拼写不同。因此,在解析过程中,and&&完全相同,只是同一事物的拼写方式不同而已。

至于它们的使用:因为它们很少被使用,所以使用它们往往比它的帮助更令人惊讶和困惑。我确信如果这是正常的,它们会更容易阅读,但人们已经习惯了 &&|| 任何其他东西都会让人分心。

编辑:然而,自从我发布此文以来,我发现它们的使用量略有增加。我仍然避开他们。

They originated in C in the header <iso646.h>. At the time there were keyboards that couldn't type the required symbols for && (for example), so the header contained #define's that would assist them in doing so, by (in our example) defining and to be &&. Of course, as time went by this became less used.

In C++, they became what are known as alternate tokens. You do not need to include anything to use these tokens in a compliant compiler (as such, the C++-ified version of the C header, <ciso646>, is blank). Alternate tokens are just like regular tokens, except for spelling. So during parsing and is exactly the same as &&, it's just a different way of spelling the same thing.

As for their use: because they are rarely used, using them is often more surprising and confusing than it is helpful. I'm sure if it were normal, they would be much easier to read, but people are so used to && and || anything else just gets distracting.

EDIT: I have seen a very slight increase in their usage since I posted this, however. I still avoid them.

离不开的别离 2024-08-30 13:10:31

它们的存在确实是为了可用性(键盘/显示器风格的字符支持)和一般可读性,但还有另一个现在更加明显的原因。 这里几乎没有答案,< a href="https://stackoverflow.com/questions/2393673/c-and-or-not-xor-keywords">此处,甚至是主要答案此处阐明了我们中的许多人更喜欢单词版本而不是符号版本的核心原因(其他语言使用它们的主要原因):错误。单词版本之间的差异非常明显。符号版本之间的差异明显较小,以至于在相对更大程度上诱人错误:“x|y”非常不是“x||y”,但是当嵌入到更大的表达式中时,我们许多人错过差异。它类似于常见的赋值运算符与相等运算符的意外混合。出于这个原因,我已经放弃了符号版本(这并不容易),转而使用文字版本。由于我们对旧事物的热爱,我宁愿有人对它们进行双重对待,而不是引诱错误。

They do exist for usability (character support in keyboard/display flavors) and general readability, but there's another reason that's nowadays more pronounced. Almost none of the answers here, here, or even the main answer here spell out the core reason many of us prefer the word versions over the symbol versions (and a main reason other languages use them): bugs. The differences between the word versions are very visible. The differences between the symbol versions are markedly less so, to the point of tempting bugs to a comparatively much greater extent: "x|y" is very much not "x||y", yet when embedded in a larger expression many of us miss the difference. It's similar to the common accidental mixing of the assignment vs equality operator. For this reason I've weaned myself off of the symbol versions (it wasn't easy) in favor of the word versions. I'd rather have someone do a double-take on them due to our love of old things than tempt bugs.

陌伤浅笑 2024-08-30 13:10:31

在 C++ 中,它们是真正的关键字。在 C 中,它们是在 中定义的宏。请参阅 http ://web.archive.org/web/20120123073126/http://www.dinkumware.com/manuals/?manual=compleat&page=iso646.html

In C++, they are real keywords. In C, they're macros defined in <iso646.h>. See http://web.archive.org/web/20120123073126/http://www.dinkumware.com/manuals/?manual=compleat&page=iso646.html.

日记撕了你也走了 2024-08-30 13:10:31

and&& 在 C++ 中功能相同。 andor 运算符是真正有效的 C++,并且是语言标准的一部分。

为了用具体的例子来详细说明其他答案,除了“可读性”之外,还有另一个原因是更喜欢 and 而不是 &&。当逻辑 AND 符合您的意思时,明确拼写出“and”可以消除出现细微错误的可能性。

考虑一下:

int x = 3;
int y = 4;

// Explicitly use "and"
if (x and y) {
    cout << "and: x and y are both non-zero values" << endl;
}

// Using "&&"
if (x && y) {
    cout << "&&: x and y are both non-zero values" << endl;
}

// Oops! I meant to type "&&"!
if (x & y) {
    cout << "&: x and y are both non-zero values" << endl;
}
else {
    cout << "How did I get here?" << endl;
}

所有三个 if 语句都会编译,但最后一个意味着完全不同且意想不到的东西!

如果您在表示逻辑 AND 时始终使用 and,则永远不会意外地键入单个“&”并让您的代码成功编译并运行并产生神秘的结果。

另一个很好的练习:尝试不小心漏掉“and”字符,看看会发生什么。 ;)

and and && are functionally the same in C++. The and and or operators are truly valid C++ and part of the language standard.

To elaborate on other answers with a concrete example, there is another reason besides just "readability" to prefer and over &&. Spelling out "and" explicitly when a logical AND is what you mean eliminates the possibility of subtle bugs.

Consider this:

int x = 3;
int y = 4;

// Explicitly use "and"
if (x and y) {
    cout << "and: x and y are both non-zero values" << endl;
}

// Using "&&"
if (x && y) {
    cout << "&&: x and y are both non-zero values" << endl;
}

// Oops! I meant to type "&&"!
if (x & y) {
    cout << "&: x and y are both non-zero values" << endl;
}
else {
    cout << "How did I get here?" << endl;
}

All three if-statements will compile, but the last one means something entirely different and unintended!

If you always use and when you mean logical AND, you can never accidentally type a single "&" and have your code compile successfully and run with mysterious results.

Another good exercise: Try leaving off a character of "and" by accident and see what happens. ;)

吐个泡泡 2024-08-30 13:10:31

尝试搜索此页面以了解逻辑运算符的用法。 &&|| 很容易找到。另一方面,搜索 andor 会给出很多误报。

符号版本也可以更好地处理纯文本文件。如果 && 出现在注释或文档中,我立即明白作者指的是逻辑运算符。如果文档中出现 ,文本样式可能会告诉我其含义。如果 and 出现在评论中...抱歉,但是单词识别并没有告诉我它在评论中的功能。

您是否看到了 的最后使用,而没有文本样式的帮助?

Try searching this page for usage of the logical operators. && or || is very easy to find. On the other hand, searching for and or or will give a lot of false positives.

The symbol versions also play more nicely with plain text files. If && appears in a comment or documentation, I immediately grok that the author meant the logical operator. If an and appears in documentation, the text styling probably tells me which is meant. If an and appears in a comment... sorry but word recognition doesn't tell me its function in the comment.

Did you even see that last use of and, without the text styling to help?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文