0

Is the word and equivalent to the && operator?

if (inner > 10 and !id)
{
    std::cout << "idle" << std::endl;
}

This code was originally translated from Python.

I was sure that the 'if' line would result in a compilation error. But it does pass.

Visual studio (2015) marks it as an error, but it does compile with g++ (and also on this site https://www.onlinegdb.com/online_c++_compiler) and seems to run as expected.

Is this correct syntax or did I miss something?

Mat
  • 202,337
  • 40
  • 393
  • 406
Ron
  • 511
  • 1
  • 5
  • 14
  • In C++ you should use `&&`, though `and` is allowed, the former is canonical http://en.cppreference.com/w/cpp/language/operator_logical – Cory Kramer Feb 15 '18 at 12:55
  • 6
    See ["alternative tokens"](http://en.cppreference.com/w/cpp/language/operator_alternative). – nwp Feb 15 '18 at 12:55
  • @CoryKramer why "should use &&" ? does it really matter? actually I dont care so much about `&&` vs `and` but sometimes I prefer a `not` to an easy to overlook `!` – 463035818_is_not_an_ai Feb 15 '18 at 12:57
  • 3
    As per https://stackoverflow.com/questions/5282692/and-or-not-versus you need to include for Visual C++ to compile this. – Garrett Gutierrez Feb 15 '18 at 13:00
  • Oddly enough **not** having `and` as a keyword is an old Microsoft extension. Really! In VS2015, if you chose Disable Language Extensions (/Za) it works, but breaks a lot of other things. In the latest VS2017 with the [/permissive-](https://blogs.msdn.microsoft.com/vcblog/2016/11/16/permissive-switch/) switch it also works, without breaking most SDK headers. – Bo Persson Feb 15 '18 at 13:28

1 Answers1

2

Yes, according to:

https://en.wikipedia.org/wiki/Digraphs_and_trigraphs#C++

and is equivalent to && ... another issue is that it is not really widely used and it is one character longer than && ... and we know that C++ programmers try to optimize everything. Even the length of their source code, so don't expect to find it widespread in production code.

Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
  • 2
    _"we know that C++ programmers try to optimize everything. Even the length of their source code"_ mmmmm.... really? Have you seen any template metaprogramming recently? It's some of the most verbose and least expressive code I've ever seen. Mind you I suppose the key word is "try".... – Lightness Races in Orbit Feb 15 '18 at 13:20
  • @LightnessRacesinOrbit I see no other reason than this urge of optimizing the source code size, that C++11 finally made it possible to **NOT** to require a space between `>` and `>` when doing stuff like: `std::vector> bb;` before you needed that wasted space :( Just imagine the amount of spaces that were saved with this change :) ... all of them can be reused somewhere else ... – Ferenc Deak Feb 15 '18 at 13:26
  • Well to be fair that was really really ugly - I don't think it was about saving a whitespace character – Lightness Races in Orbit Feb 15 '18 at 14:20
  • certainly not. I tried to keep my comment in the spirit of the answer :) I always seem to appreciate a sense of humor in fellow programmers :D – Ferenc Deak Feb 15 '18 at 14:22