#define test 1 #pragma PRAGMA_MESSAGE( STRINGIZE(test) ) #pragma push_macro("test") #undef test #define test 2 #pragma PRAGMA_MESSAGE( STRINGIZE(test) ) #pragma pop_macro("test") #pragma PRAGMA_MESSAGE( STRINGIZE(test) )outputs : 1 , 2 , 1
Wow!
BTW this demo used these tricks :
#define _Stringize( L ) #L #define _DoMacro1( M, X ) M(X) #define STRINGIZE(M) _DoMacro1( _Stringize, M ) #define LINE_STRING STRINGIZE( __LINE__ ) #define PRAGMA_MESSAGE(str) message( __FILE__ "(" LINE_STRING ") : message: " str)
Of course I can't use it at work where multi-platform support is important, but I can use it at home where I don't give a flying fuck about things that don't work in MSVC and it makes life much easier.
5 comments:
GCC supports that as well:
http://gcc.gnu.org/onlinedocs/gcc/Push_002fPop-Macro-Pragmas.html
#define old_test test
#undef test
#define test whatever i want to do
#undef test
#define test old_test
Obviously what 800poundgames wrote doesn't work. There is a strategy to avoid the mess of knowing how to define it back to the original def, although you probably already know this:
#define THINGY_publicdef ...actual definition...
#define THINGY THINGY_publicdef
then in a place where you need to override it:
#undef THINGY
#define THINGY whatever
and then to undo it
#undef THINGY
#define THINGY THINGY_publicdef
Yeah, Sean, obviously that is what I do, but that doesn't work when you don't know which publicdef to do, eg. when you're mixing codebases.
The most common case where I hit this is with malloc or new. I think I'll write more about that case, but the basic issue is :
#undef new
.. do stuff with original new
#define new my_new
but if this is like inside an STL header, it can't know to use the "my_new" from cblib, etc.
BTW I wish you could pragmas inside #defines ; there's really no reason why not, #define is just text-sub.
Post a Comment