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?
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.
You ask about the rationale. Here's one possible reason, not necessarily the one that most influenced the Visual C++ team:
/permissive-
or /Za
for maximum conformance anyway, which will cause these to be treated as keywords./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.)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.
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>
(orciso646
) 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With