可以用“and”、“or”来表示吗?等等,而不是“&&”,“||”?

发布于 2024-10-04 13:56:48 字数 274 浏览 7 评论 0原文

我习惯了 C++ 中的 andor 关键字。我一直在使用它们,并且打字对我来说既快速又舒适。我听说这些别名是非标准的,可能不适用于所有编译器。但我不确定,我真的不知道这是不是真的。
假设我将代码交给某人,他在编译时会遇到问题吗?
我用andor代替&&||可以吗?或者这些关键字真的不标准吗?
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 技术交流群。

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

发布评论

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

评论(10

无妨# 2024-10-11 13:56:49

这些关键字是标准的,并在标准的 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.

ぺ禁宫浮华殁 2024-10-11 13:56:49

它们是新的 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.

油饼 2024-10-11 13:56:49

它们是标准的 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.,

心意如水 2024-10-11 13:56:49

我总是搞乱 ^(异或)和 ~(两个补码)运算符。对于替代标记(我认为应该是主要标记),它们的作用是毫无疑问的,是的,我同意以前的发帖者的观点,即文本标记更具描述性。

使用二合字母还有另一种可能的混乱,可能会忘记 ||、&& 中的字符之一。这会导致微妙的错误和奇怪的行为。
使用文本运算符,就更难犯这样的错误。

我相信我上面提到的这些都是提高代码安全性和清晰度的真正有效的论据。在我看来,大多数 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.

木落 2024-10-11 13:56:49

它在语法上是有效的,因为这些是必需的替代标记

在语言的所有方面,每个替代标记的行为分别与其主要标记相同,但拼写除外。

然而,某些标记不仅仅用于逻辑或按位运算符。所以人们可以看到其特质:

auto& foo(auto and T) { // C++20 forwarding reference
    return T;
} 

甚至

auto& foo(auto bitand T) { // lvalue reference
    return T;
} 

这会让人们摸不着头脑。

It's syntactically valid, given those are required alternative tokens.

In all respects of the language, each alternative token behaves the same, respectively, as its primary token, except for its spelling.

However, some tokens are used for more than just logical or bitwise operators. So one can see the idiosyncratic:

auto& foo(auto and T) { // C++20 forwarding reference
    return T;
} 

Or even

auto& foo(auto bitand T) { // lvalue reference
    return T;
} 

That's gonna make one scratch their head for a while.

乞讨 2024-10-11 13:56:49

哇,多年来我一直在使用和查看许多 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 ||.

深海蓝天 2024-10-11 13:56:49

ISO/IEC 14882:1998 标准(原始 C++ 标准)的第 2.5 节说:

§2.5 替代标记 [lex.digraph]

1 为某些运算符和标点符号提供了替代标记表示16)

2 在语言的所有方面,每个替代标记的行为分别与其主要标记相同,
除了其拼写17)。替代令牌集在表 2 中定义。

16) 其中包括“二合字母”和其他保留字。术语“二合字母”(由两个字符组成的标记)并不完美
描述性的,因为替代预处理标记之一
是 %:%: 当然,几个主要标记包含两个字符。
尽管如此,那些不是词汇关键字的替代标记通俗地称为“二合字母”。

17) 因此,[ 和 <: 的“字符串化”值 (16.3.2) 将不同,保持源拼写,但标记可以以其他方式更改
自由互换。

                 Table 2—alternative tokens
_______________________________________________________________________________
    alternative  primary  |  alternative  primary  |  alternative  primary
     <%            {      |     and          &&    |    and_eq       &=
     %>            }      |    bitor         |     |    or_eq        |=
     <:            [      |     or           ||    |    xor_eq       ^=
     :>            ]      |     xor          ^     |     not         !
     %:            #      |    compl         ~     |    not_eq       !=
     %:%:          ##     |    bitand        &     |
_______________________________________________________________________________

没有讨论“如果包含一些标头”(尽管在 C 中,您需要 #include)。任何不支持关键字或二合字母的实现都不符合 1998 年版本的 C++ 标准,更不用说更高版本了。

Section 2.5 of the ISO/IEC 14882:1998 standard (the original C++ standard) says:

§2.5 Alternative tokens [lex.digraph]

1 Alternative token representations are provided for some operators and punctuators16).

2 In all respects of the language, each alternative token behaves the same, respectively, as its primary token,
except for its spelling17). The set of alternative tokens is defined in Table 2.

16) These include “digraphs” and additional reserved words. The term “digraph” (token consisting of two characters) is not perfectly
descriptive, since one of the alternative preprocessing tokens
is %:%: and of course several primary tokens contain two characters.
Nonetheless, those alternative tokens that aren’t lexical keywords are colloquially known as “digraphs”.

17) Thus the “stringized” values (16.3.2) of [ and <: will be different, maintaining the source spelling, but the tokens can otherwise be
freely interchanged.

                 Table 2—alternative tokens
_______________________________________________________________________________
    alternative  primary  |  alternative  primary  |  alternative  primary
     <%            {      |     and          &&    |    and_eq       &=
     %>            }      |    bitor         |     |    or_eq        |=
     <:            [      |     or           ||    |    xor_eq       ^=
     :>            ]      |     xor          ^     |     not         !
     %:            #      |    compl         ~     |    not_eq       !=
     %:%:          ##     |    bitand        &     |
_______________________________________________________________________________

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.

就像说晚安 2024-10-11 13:56:49

显然,就向后兼容性而言,“和/或”关键字不是问题。我相信它们是更新的标准。只是老程序员不明白,一些菜鸟可能必须能够阅读代码,而不想查找 && 的内容。方法。话又说回来,如果任何 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).

假情假意假温柔 2024-10-11 13:56:49

您还可以使用“*”代替 &&和“+”而不是 ||

You can also use '*' instead of && and '+' instead of ||

清风无影 2024-10-11 13:56:48

事实上,它们是 C++ 中的标准,由 ISO 14882:2003 C++ 标准 2.5/2 定义(实际上,由 1998 版标准定义)。请注意,它们内置于语言本身中,不需要您包含某种头文件。

然而,它们很少被使用,而且我还没有看到实际使用替代令牌的生产代码。替代标记存在的唯一原因是因为某些键盘(尤其是非 QWERTY 键盘)上的这些字符要么不存在,要么输入起来很笨拙。它仍然在向后兼容的标准中。

尽管它们是标准的,但我强烈建议您不要使用它们。替代标记需要输入更多字符,并且 QWERTY 键盘布局已经具有键入 C++ 代码所需的所有字符,而无需使用替代标记。此外,它们很可能会让代码的读者感到困惑。

2.5/2 替代代币

在语言的各个方面,每个
替代令牌的行为相同,
分别作为其主要代币,
除了它的拼写。的集合
替代令牌在表中定义
2.

Table 2 - alternative tokens
+--------------+-----------+
| Alternative  |  Primary  |
+--------------+-----------+
|    <%        |    {      |
|    %>        |    }      |
|    <:        |    [      |
|    :>        |    ]      |
|    %:        |    #      |
|    %:%:      |    ##     |
|    and       |    &&     |
|    bitor     |    |      |
|    or        |    ||     |
|    xor       |    ^      |
|    compl     |    ~      |
|    bitand    |    &      |
|    and_eq    |    &=     |
|    or_eq     |    |=     |
|    xor_eq    |    ^=     |
|    not       |    !      |
|    not_eq    |    !=     |
+--------------+-----------+

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.

2.5/2 Alternative tokens

In all respects of the language, each
alternative token behaves the same,
respectively, as its primary token,
except for its spelling. The set of
alternative tokens is defined in Table
2.

Table 2 - alternative tokens
+--------------+-----------+
| Alternative  |  Primary  |
+--------------+-----------+
|    <%        |    {      |
|    %>        |    }      |
|    <:        |    [      |
|    :>        |    ]      |
|    %:        |    #      |
|    %:%:      |    ##     |
|    and       |    &&     |
|    bitor     |    |      |
|    or        |    ||     |
|    xor       |    ^      |
|    compl     |    ~      |
|    bitand    |    &      |
|    and_eq    |    &=     |
|    or_eq     |    |=     |
|    xor_eq    |    ^=     |
|    not       |    !      |
|    not_eq    |    !=     |
+--------------+-----------+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文