Search code examples
c++operator-keyword

What is the difference between "and" and "&&" in c++


Recently I found a code where is used the keyword and which working like &&. So are they both the same or is there any specific condition to use it?


Solution

  • The C++ standard permits the token && to be used interchangeably with the token and.

    Not all compilers implement this correctly (some don't bother at all; others require the inclusion of a special header). As such, code using and can be considered idiosyncratic.

    The fact that the equivalence is at the token, rather than the operator, level means that since C++11 (where the language acquired the rvalue reference notation), you can arrange things (without recourse to the preprocessor) such that the statement

    int and _int(string and vector);
    

    is a valid function prototype. (It's eqivalent to int&& _int(string&& vector).)