C++ – Why Visual Studio Does Not Define Alternative Tokens for Logical Operators

c++visual-c++

Alternative tokens are valid c++ keywords, yet in Visual Studio 2013 the following emits a compilation error (undeclared identifier):

int main(int argc, const char* argv[])
{
    int k(1), l(2);
    if (k and l) cout << "both non zero\n";

    return 0;
}

Since and or not have been around for quite some time, is there a reason for not implementing them?

Best Answer

You ask about the rationale. Here's one possible reason, not necessarily the one that most influenced the Visual C++ team:

  1. Those are valid identifiers in C.
  2. Microsoft's recommendation has long been to use C++ mode for both C and C++ code, rather than maintaining a modern C compiler.
  3. Valid C code using these as identifiers would gratuitously break if they were compiled as keywords.
  4. People trying to write portable C++ are mostly using /permissive- or /Za for maximum conformance anyway, which will cause these to be treated as keywords.
  5. The workaround to treat them as keywords in /Ze by including a header file is easy and portable. (G++'s workaround -fno-operator-names isn't bad either, but putting the option in the source code rather than the build system is somewhat nicer.)
Related Question