0

I was wondering if it was possible to define a macro in C++ that defines another macro that can be used in later code. Is this possible, or is the preprocessor used by g++ too limited for this?

2
  • 1
    It's nor the "preprocesor used by g++" that isn't powerful enough, it's the C++ language itself that does not allow this. If you explained what you want to accomplish, I'm sure people here could come up with a solution. Commented Jan 20, 2012 at 15:06
  • How would you be "limited" by this? Share what you're trying to accomplish. Commented Jan 20, 2012 at 15:31

4 Answers 4

5

No, you cannot define a macro within the expansion of another macro.

Sign up to request clarification or add additional context in comments.

Comments

1

Nope, you can't define a macro as a macro.

Comments

1

The preprocessor makes only one pass over the source code, so this is not possible. However, you could use an external tool to perform some preprocessing ahead of compilation, like m4.

Comments

1

You can do something like this, its not exactly what you are looking for, but it might help.

#ifdef ENABLE_MACRO_1
#define PRINT_MACRO(varName)   \
        std::cout<<varName<<std::endl;
#else
#define PRINT_MACRO(varName)   \
        //do nothing
#endif

So you can define a macro depending on another preprecursor condition which was defined defined.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.