Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserving preprocessor definitions [duplicate]

Possible Duplicate:
Can I redefine a C++ macro then define it back?

Say I have some code that uses the name BLAH for a variable. Suppose BLAH is a common preprocessor definition in many standard header files (defined as 10), so if my file is included after any of them, the code breaks because BLAH is transformed into 10; therefore, I must #undef BLAH. But also other headers may depend on BLAH, so I must restore BLAH to it's original value after my header is done. Is it possible to do something like this:

#ifdef BLAH
#define BLAH_OLD BLAH
#undef BLAH
#endif

... code ...

// restore BLAH to 10
#ifdef BLAH_OLD
#define BLAH BLAH_OLD
#end

? This doesn't work of course, because BLAH is not expanded to 10. I have tried doing something like

#define EXPAND_AGAIN(x) x
#define EXPAND(x) EXPAND_AGAIN(x)
#define BLAH_OLD EXPAND(BLAH)

but that doesn't work either, since EXPAND is taken literally and not expanded. I am using MSVC 2008/2010, but it would be lovely if the solution would work on most other compilers too.

like image 793
Epro Avatar asked Nov 08 '10 21:11

Epro


2 Answers

Yes, given that your compiler supports the push/pop macro directives (visual c++, gcc, llvm all do):

#define BLAH 10

#pragma push_macro("BLAH")
#undef BLAH

#define BLAH 5

...

#pragma pop_macro("BLAH")
like image 153
yonilevy Avatar answered Oct 21 '22 06:10

yonilevy


Unfortunately the preprocessor doesn't support a stack of definitions.

The Boost preprocessor library makes the preprocessor do things you'd never imagine it could do (like effectively variadic macros in C++98), but is bound by the inherent limitations of the preprocessor -- so, no can do, sorry.

The only known half-way remedy is to reserve ALL_UPPERCASE_IDENTIFIERS for macros, and consistently always use them for macros. It reduces the name collision problem somewhat. Unfortunately the C standard library defines a number of lowercase macros, or allows for their existence, like e.g. assert, but they are just a few.

From a practical point of view the main problem is in Windows programming, where Microsoft's [windows.h] header defines zillions of non-uppercase macros, including, by default, min and max which conflict with the C++ standard library.

So, for Windows C++ programming, always define NOMINMAX before including [windows.h].

Cheers & hth.,

like image 4
Cheers and hth. - Alf Avatar answered Oct 21 '22 05:10

Cheers and hth. - Alf