flyout
flyout

Reputation: 497

Can I use #undef this way?

I want to get some settings I store in the registry, and if they differ from a #define I want to redefine it, could I do it this way?:

#define DEFINED_X "testSetting"

void LoadConfig()
{
    regConfigX = some value previusly stored in the registry;
    if(regConfigX!=DEFINED_X)
    {
        #undef DEFINED_X
        #define DEFINED_X regConfigX
    }
}

I tought #define was used only when compiling, would this code work when running the compiled exe?

Upvotes: 0

Views: 153

Answers (3)

Puppy
Puppy

Reputation: 146998

#define and #undef occur before your source code even hits the compiler. Anything to do with #defines can't happen at runtime.

You should check out the Boost preprocessor library, too.

Upvotes: 1

Pierre-Antoine LaFayette
Pierre-Antoine LaFayette

Reputation: 24412

No, use a static variable to store the value of DEFINED_X.

Upvotes: 0

James McNellis
James McNellis

Reputation: 355267

No. #define and #undef are preprocessing directives; they are evaluated before the source code is compiled.

You need to use a variable for this, not a macro.

Upvotes: 1

Related Questions