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)
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)
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.
I prefer &&
instead of and
.
&&
is widely known and accepted, while many don't even know that and
is valid C++.and
(and friends) by default. For example MSVC++.&&
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.
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..
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.