msc
msc

Reputation: 34588

Can a macro call a preprocessor command?

Can a macro call a preprocessor command?

For example, can I write something like,

#define PreProcessor(x, y)  #define x ((y)+1)

Upvotes: 1

Views: 58

Answers (1)

user2512323
user2512323

Reputation:

It is not possible to expand a macro into something that is also a preprocessor directive, as in §6.10.3.4, 3:

The resulting completely macro-replaced preprocessing token sequence is not processed as a preprocessing directive even if it resembles one...

But, it is possible to conditionally define macro itself:

#if CONDITION_A_IS_MET
#define x ((y)+1)
#else
#define x /*...some other definition*/
#endif

Or use an X-macro:

#define PreProcessor(x) X(x, ((x) + 1))

/*...later*/

#define X(a, b) printf("%d, %d", a, b)
PreProcessor(5) /* Outputs 5, 6 */

To cover most of the common cases for that feature.

Upvotes: 3

Related Questions