16

I saw alternative operators (like and, or, not etc.) when browsing cppreference.

They are alternatives to "normal" operators like &&, ||, ! etc.

I examined the assembly for code that uses && and and. Both versions generated the same assembly.

Code :

#include <iostream> 

int n = 1;
int main()
{
// if(n > 0 && n < 5)
   if(n > 0 and n < 5)
   {
       std::cout << "n is small and positive\n";
   }
}

So my questions are:

  • What is the difference between the && and and operators?
  • Where and when do I use and over &&?
  • If there is no difference, then why does C++ introduce alternative operators (like and, or, not etc.)?
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
msc
  • 33,420
  • 29
  • 119
  • 214

2 Answers2

19

What is the difference between the && and and operators?

There is none1. The "alternative" aspect of these operators means that they can be used to construct the exact same expressions from a semantic perspective.

Where and when do I use and over &&?

This is largely a matter of preference. I'm too used to && to not use it, but can understand if someone finds and more readable.

why does C++ introduce alternative operators?

C++ was designed to be available on a variety of character sets and platforms. Trigraphs, like Bathsheba pointed out, are another example of such a feature. If a character set would not allow && to be written (say, because it simply didn't have the & character) then one can still get by with the alternative representation. Nowadays, it's largely moot.


1 Actually, upon further thinking, my answer to your first question can be refined. There is a slight lack of equivalence, pertaining to how tokens are parsed. && doesn't require a space to be parsed as a separate token, while and does. That means:

void foo(bool b1, bool b2) {
  if(b1&&b2) { // Well formed
  }

  if(b1andb2) { // ill formed, needs spaces around `and`
 }
}

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • Has "and" always existed in c++? I don't recall seeing anything other than && – Carlos Nov 08 '17 at 08:39
  • 1
    @Carlos - I can tell you it exists since at least [C++11](https://timsong-cpp.github.io/cppwp/n3337/lex.key#2). I don't have something close enough to C++03 to compare. – StoryTeller - Unslander Monica Nov 08 '17 at 08:41
  • 2
    They are present from the very first C++ standard - https://stackoverflow.com/questions/4251699/is-it-okay-to-use-and-or-etc-instead-of but they are so uncommon that VS doesn't enable them by default - https://stackoverflow.com/questions/3448279/and-or-instead-of-in-c-code-compiler-feature-or-programmers-f – taskinoor Nov 08 '17 at 08:51
  • @taskinoor: I believe VS implement them using macros, which wreaks havoc in some edge cases. – Bathsheba Nov 08 '17 at 08:52
  • 1
    @Bathsheba - When does "VS" and "macros" in the same sentence does not spell doom? `min` anyone? – StoryTeller - Unslander Monica Nov 08 '17 at 08:53
  • @Bathsheba Which edge cases? There should be no difference for all intents and purposes. The only thing I could see is that it would make error messages slightly more confusing if somebody attempted to use them as identifiers. But it should never change the validity of a program. – Konrad Rudolph Nov 08 '17 at 10:14
  • 1
    Sir, nice answer. Thank you very much. – msc Nov 08 '17 at 10:19
  • @KonradRudolph Stringization, for one. – T.C. Nov 08 '17 at 13:09
  • @T.C. I’m not convinced there’s a problem: It’s unspecified how the alternative tokens are stringised anyway, so whatever MSVC does would be fine. – Konrad Rudolph Nov 08 '17 at 13:42
  • @KonradRudolph "It's unspecified how the alternative tokens are stringised anyway" {{citation needed}} – T.C. Nov 08 '17 at 14:46
  • @T.C. That would be hard, since it’s precisely *not specified*. The inverse works, though; so: please show me where it’s specified (I’m serious: it’s entirely possible that I’ve overlooked something, though I don’t think so). – Konrad Rudolph Nov 08 '17 at 15:01
  • @KonradRudolph how does [cpp.stringize]/2 not cover it? – T.C. Nov 08 '17 at 21:42
  • @T.C. I don’t think so. What is the “spelling” of an alternative token, such as `and`? Is it “and” or “&&”? I think both would be valid (and MSVC will give you one or the other, depending on whether you stringise the token directly or expanded in a macro argument). – Konrad Rudolph Nov 09 '17 at 10:57
  • 1
    @KonradRudolph It seems common sense to me that the "spelling" of a token is the way it appears in the source text. See also https://timsong-cpp.github.io/cppwp/lex.digraph#2.sentence-1 and the accompanying footnote. – T.C. Nov 09 '17 at 18:51
  • @T.C. Very good find! Thanks. – Konrad Rudolph Nov 09 '17 at 21:53
10

and, or, not, &c. are examples of the alternative operators.

For the full list see http://en.cppreference.com/w/cpp/language/operator_alternative; the opening paragraph is a raison d'etre:

C++ (and C) source code may be written in any non-ASCII 7-bit character set that includes the ISO 646:1983 invariant character set. However, several C++ operators and punctuators require characters that are outside of the ISO 646 codeset: {, }, [, ], #, \, ^, |, ~. To be able to use character encodings where some or all of these symbols do not exist (such as the German DIN 66003), C++ defines the following alternatives composed of ISO 646 compatible characters.

If I were you I'd shy away from using them, much in the same way as you ought to shy away from using digraphs and trigraphs, even if the latter make for interview fun.

Trigraphs for example are explicitly discontinued from C++17. You might see and &c. being dropped in future standards too.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483