15

I'm using C++11 and both are compiling without any warning, witch one is the best way to do it?

if(a && b)

or

if(a and b)
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142

4 Answers4

17

2.6 Alternative tokens [lex.digraph]

1 Alternative token representations are provided for some operators and punctuators.16

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

Can't paste table 2, but it explicitly states Alternative: and, Primary && (same for or and ||).

So they are absolutely identical. If you want to try and convince yourself one is "better" than the other, that's your business. If someone else is trying to argue such, they'd better have a good reason.

Edit: The aforementioned Table 2:

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

Edit: Maybe worth noting, according to Sebastian Redl, MS break the rules here.

Community
  • 1
  • 1
BoBTFish
  • 19,167
  • 3
  • 49
  • 76
  • So would that imply that it is legal, given `int x,y;`, to say `memcpy(bitand x, bitand y, sizeof x);`? – supercat Sep 13 '16 at 17:34
  • @supercat Legal, yes. You may even use `compl` to declare a destructor. But if I'm doing your code review, I may not be very polite about it! – BoBTFish Sep 14 '16 at 06:38
8

I prefer && instead of and.

  • && is widely known and accepted, while many don't even know that and is valid C++.
  • Some IDEs don't accept and (and friends) by default. For example MSVC++.
  • At least for me, the operator precedence of && and || is ingrained into my head. While and and or have the same precedences as && and ||, the simple fact that I'm much less used to them makes it harder to read a condition.

On the other hand, and is more verbose and might be easier to use for programmers who have learned programming with languages that don't use &&. But one could argue that these people should learn C++ rather than try to change it's snytax.

Community
  • 1
  • 1
s3rius
  • 1,442
  • 1
  • 14
  • 26
5

I would prefer if(a and b) , because there is always the chance to mix up accidently if(a && b) with if(a & b) , causing you a lot of trouble..

fiscblog
  • 694
  • 1
  • 12
  • 27
  • 1
    Hmm... I wonder about consistency with `operator&&` ? There is no operator overload for `operatorand`. (I wonder how that would interact with the tokenizer if you tried to `operator and`?) – Andre Kostur Jun 14 '13 at 14:19
  • For the operator && there isn't an overload either, is it? – fiscblog Jun 14 '13 at 14:22
  • 5
    Yep, there is. You can overload `&&` for your own classes. The extra gotcha with that particular overload is that it disables short-circuit evaluation for `&&` (and similarly `||`) when used with your class (it becomes a normal function invocation, both parameters need to be evaluated before calling the function). – Andre Kostur Jun 14 '13 at 14:25
  • Good point, I hadn't considered that the difference between `bitand` and `and` is more obvious than `&&` and `&`. I'm sure this has bitten plenty of people in the past (though I can't think of an example in my code). – jerry Jun 14 '13 at 14:32
  • 1
    Tried the `operator and` with clang, and it is a tokenizer thing. If you do `operatorand`, the tokenizer sees it as one token and uses it as a new function name. If you do `operator and`, it sees them as two separate tokens and treats it the same as `operator &&` (and will even complain of a duplicate declaration if you try to declare both). – Andre Kostur Jun 14 '13 at 14:37
4

As someone who programs in both C and C++, unless there's a good reason to use different alternatives in each language, I prefer to keep it consistent. Although and has been a part of the C standard for close to two decades, it requires a header file instead of being built into the language. Especially when a piece of code may be used in multiple projects, the hassle is just not worth it.

I've never seen a situation where using and over && would be advantageous. I can't imagine a modern development system without a & key, though maybe if you're trying to do something on an unusual platform (like directly programming on a severely limited mobile/embedded system) it would be useful. I also think it reduces the readability of my code for people who are very used to seeing && as the logical and operator.

jerry
  • 2,581
  • 1
  • 21
  • 32