Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does VS not define the alternative tokens for logical operators?

Tags:

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?

like image 896
Nikos Athanasiou Avatar asked Jun 25 '14 16:06

Nikos Athanasiou


4 Answers

VS is nonconforming. This is old news.

To use alternative tokens, include the <ciso646> header. According to the standard, including this header is supposed to have no effect in C++. However, you do need it in VS. So it's safe to just include it always, whenever there is any chance that you might be compiling with VS.

like image 29
Brian Bi Avatar answered Oct 24 '22 05:10

Brian Bi


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.)
like image 113
Ben Voigt Avatar answered Oct 24 '22 06:10

Ben Voigt


Formally, these keywords are implemented and are supported intrinsically by the compiler without including any headers. However, for that you have to compile your source code in "more standard" mode of that C++ compiler, which means using the /Za option.

By intent, the /Za option is supposed to "disable compiler extensions". Of course, not supporting something that is supposed to be there in a compliant compiler cannot be formally qualified as a "compiler extension". Yet, that just the way things currently are.

like image 33
AnT Avatar answered Oct 24 '22 07:10

AnT


Modern Visual Studio (or rather, MSVC) does support alternative tokens; however, it does so only in standards conformance mode. This mode comes with lots of goodies, so it should always be used.

To enable it, pass /permissive- to the compiler.


The answer below is obsolete. This is now supported, see above!

Previously, Microsoft’s position was1 that

#include of <iso646.h> (or ciso646) is how we support these keywords

And because “nobody” (before me, in 2007) ever requested this.

1 link currently defunct; left here for archival purpose.

like image 24
Konrad Rudolph Avatar answered Oct 24 '22 07:10

Konrad Rudolph