Getting around VS2012 C1189/C4005 error: keyword re-defined in macro(s)

For the impatient (read legacy code maintainers): Go ahead and add _ALLOW_KEYWORD_MACROS to your list of preprocessor definitions in the project settings to get rid of the errors.

If you’re still here and wondering what hit you: VS2012 bids farewell to all the macro magic you did all this while. Including the (now) infamous #define private public.

But then there is legacy code and legacy macros to get around limitations of those legacy compilers. And you live with them. Until you decide you want to move up in life and switch to the latest and greatest compiler. You then hit:

C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xkeycheck.h(124): fatal error C1189: #error : keyword defined before including C++ standard header

and an additional C4005 (just to make things interesting).

There’s the tech-note on Breaking Changes in Visual C++ for VS 2012 where somewhere towards the bottom you have the innocuous looking paragraph:

C++11 [macro.names]/2 forbids macro-izing keywords when C++ Standard Library headers are included. The headers now emit compiler errors if they detect macro-ized keywords. (Defining _ALLOW_KEYWORD_MACROS allows such code to compile, but we strongly discourage that usage.) As an exception, macro-ized new is permitted by default, because the headers comprehensively defend themselves by using #pragma push_macro(“new”)/#undef new/#pragma pop_macro(“new”). Defining _ENFORCE_BAN_OF_MACRO_NEW does exactly what its name implies.

Now, if you didn’t look that up, you’d still be scratching your head!


About this entry