saving a #define before an #undef

  • Thread starter myheartinamerica
  • Start date
M

myheartinamerica

Hello,

I was working on a piece of code where a vendor's #define was causing
compiler warnings, which led me to try and do the following:

#if defined(VAR1)
#define VAR1_SAVE VAR1
#undef VAR1
#endif
#define VAR1 1
#define VAR2 (10 - VAR1)
#undef VAR1
#if defined(VAR1_SAVE)
#define VAR1 VAR1_SAVE
#undef VAR1_SAVE
#endif

Even if this is ridiculous, I'm unsure as to why it didn't work. Any
insights? I needed VAR2 to be #defined in terms of VAR1, but I needed
it to be my own locally #define'd VAR1.
 
P

Peter Nilsson

myheartinamerica said:
I was working on a piece of code where a vendor's #define
was causing compiler warnings, which led me to try and do
the following:

#if defined(VAR1)
    #define VAR1_SAVE VAR1

This will cause VAR1_SAVE to expand to, literally, VAR1.
    #undef VAR1

This won't change what VAR1_SAVE will expand to.

What it will do is remove any (further) substitution for
VAR1.
#endif
#define VAR1 1
#define VAR2 (10 - VAR1)
#undef VAR1
#if defined(VAR1_SAVE)
    #define VAR1 VAR1_SAVE
    #undef VAR1_SAVE
#endif

Even if this is ridiculous, I'm unsure as to why it
didn't work.

Because macros are not 'variables'.
Any insights? I needed VAR2 to be #defined in terms of
VAR1, but I needed it to be my own locally #define'd VAR1.

Either rename the third party VAR1 or change yours.

As a style point, many people adopt naming conventions
to mitigate (but not preclude) such namespace clash issues.
 
M

myheartinamerica

#if defined(VAR1)
This will cause VAR1_SAVE to expand to, literally, VAR1.

Much clearer now. Thanks. As far as naming conventions and style
points go, I've found that commercial code leaves something to be
desired. Basically I want to shoot my fucking face off every time I go
to work.

Thanks again for the preprocessor help, though.

-Mick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top