Linuxios
Linuxios

Reputation: 35793

Can macros in C++ define macros?

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?

Upvotes: 0

Views: 200

Answers (4)

tune2fs
tune2fs

Reputation: 7705

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.

Upvotes: 1

Luc Touraille
Luc Touraille

Reputation: 82041

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.

Upvotes: 1

Jdc1197
Jdc1197

Reputation: 255

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

Upvotes: 1

Anthony Williams
Anthony Williams

Reputation: 68571

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

Upvotes: 5

Related Questions