简体   繁体   English

可以用“and”、“or”等代替“&&”、“||”吗?

[英]Is it okay to use "and", "or" etc. instead of "&&", "||"?

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.我已经习惯了 C++ 中的andor关键字。我一直使用它们,并且输入它们对我来说又快又舒服。 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 && , ||当我使用and or代替&&时可以吗|| ? Or are these keywords really non-standard?还是这些关键词真的不规范?
PSI use the MinGW compiler. PSI 使用 MinGW 编译器。

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). 它们实际上是C ++中的标准,正如ISO 14882:2003 C ++标准2.5 / 2所定义的那样(实际上,正如1998年版标准所定义的那样)。 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. 替代令牌首先存在的唯一原因是因为某些键盘上的这些字符(特别是非QWERTY键盘)要么不存在要么笨拙要键入。 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. 替代标记需要输入更多字符,QWERTY键盘布局已经具有输入C ++代码所需的所有字符,而无需使用替代标记。 Also, they would most likely bewilder readers of your code. 此外,他们很可能会对您的代码感到困惑。

2.5/2 Alternative tokens 2.5 / 2替代代币

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. 表2中定义了替代令牌集。

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

These keywords ARE standard and are described in section 2.5 of the standard. 这些关键字是标准的,并在标准的第2.5节中描述。 Table 2 is a table of these "alternative tokens". 表2是这些“替代令牌”的表格。 You can use them all you want, even though everyone will hate you if you do. 您可以随心所欲地使用它们,即使每个人都会恨你。

They are standard in the new c++0x standard. 它们是新c ++ 0x标准的标准配置。 Up-to-date modern compilers should recognise them, although I don't believe they are obliged to yet. 最新的现代编译器应该认识到它们,尽管我认为它们还没有义务。 Whatever floats your boat, I assume. 无论你的船是什么漂浮,我都认为。

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] 它们是标准的C ++,但是对于较旧的编译器,可能还有MSVC 10.0(我还没有检查过),你可能需要包含一个特殊的标题,[isosomethingsomething.h]

cheers & hth., 干杯和hth。,

I have always messed up ^ (xor) and the ~ (two complement) operators. 我总是弄乱^(xor)和〜(两个补码)运算符。 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. 在我看来,大多数C ++程序员应该尝试习惯于文本操作符,而不是旧的神秘操作符。

I am surprised that so few programmers know about them. 我很惊讶很少有程序员了解它们。 These operators should have taken over long time ago as I see it. 这些操作员应该在我看来很久以前就已经接管了。

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. 哇,我多年来一直在使用和查看许多C ++代码示例..直到现在,才知道这些,所以我猜这意味着大多数人都不使用它们。 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 ||. 因此,为了保持一致性(如果您计划在小组项目中工作等),最好养成使用&&和||的习惯。

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.这会让一个人挠头一阵子。

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! 然后,如果任何IT部门值得它的盐,它将使程序员符合公司的标准! That is my belief so (and/or) are futuristic and real possible standard going towards the future. 这是我的信念所以(和/或)是面向未来的未来主义和真实可能的标准。 && is backward compatable not(pun) (and/or). &&是向后兼容的不是(双关语)(和/或)。

Section 2.5 of the ISO/IEC 14882:1998 standard (the original C++ standard) says: ISO / IEC 14882:1998标准(原始C ++标准)第2.5节说:

§2.5 Alternative tokens [lex.digraph] §2.5替代令牌[lex.digraph]

1 Alternative token representations are provided for some operators and punctuators 16) . 1为某些运营商和标点符号提供了替代的令牌表示16)

2 In all respects of the language, each alternative token behaves the same, respectively, as its primary token, except for its spelling 17) . 2在语言的所有方面,每个备用令牌的行为分别与其主令牌相同,但拼写除外17) The set of alternative tokens is defined in Table 2. 表2中定义了替代令牌集。

16) These include “digraphs” and additional reserved words. 16)这些包括“有向图”和其他保留字。 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. 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        &     |
_______________________________________________________________________________

There is no discussion of 'if you include some header' (though in C, you need #include <iso646.h> ). 没有讨论'如果你包含一些标题'(虽然在C中,你需要#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. 任何不支持关键字或有向图的实现都不符合C ++标准的1998版,更不用说更高版本了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否优先使用结构初始化({...})而不是memset等? - Is it preferred/okay to use structure-initialization ({…}) over memset, etc.? 使用包含%1,%2等的格式字符串,而不是%d,%s等。 - Linux,C ++ - Use format strings that contain %1, %2 etc. instead of %d, %s etc. - Linux, C++ 如何在C ++ mongodb驱动程序中使用$ set,$ inc等 - how to use $set, $inc etc. in c++ mongodb driver 是否建议在C ++中使用cstdio,cstring,cmath等? - Is it recommended to use cstdio, cstring, cmath, etc. in C++? 使用 ffmpeg - % 等的转码转换器的进度使用什么单位? - What units for the progress to use for a transcoding converter using ffmpeg - % etc.? 可以在循环中使用int而不是size_t吗? - Is it okay to use int instead of a size_t in looping over a vector? 为什么这个令人困惑的#include“ .. \\ .. \\ [etc。 等等]”的声明有效吗? - Why can this confusing #include“..\..\[etc. etc.]” statement work? 使用 python 绑定到 clang 以规范方式检查 C++ 类型是否是指针、引用等? - Use python bindings to clang to check if a C++ type is a pointer, reference, etc. in canonical fashion? 有没有办法转发所有赋值运算符(+ =,* =等)以隐式使用重写的直接赋值运算符(=)? - Is there a way to forward all assignment operators (+=, *=, etc.) to implicitly use an overridden direct assignment operator (=)? 返回值是class还是class时如何使用enable_if <class> 或课程 <class, class> 等等? - How to use enable_if when return value is either class or class<class> or class<class, class>, etc.?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM