Combining two #defined characters in a C ++ preprocessor

I want to make:

#define VERSION XY123 #define PRODUCT MyApplication_VERSION 

so PRODUCT is actually MyApplication_XY123. I tried to play with the merge operator ##, but with limited success ...

 #define VERSION XY123 #define PRODUCT MyApplication_##VERSION 

=> MyApplication_VERSION

 #define VERSION XY123 #define PRODUCT MyApplication_##(VERSION) 

=> MyApplication_ (XY123) - close, but not quite

I want this to be possible?

+8
c ++ c-preprocessor
source share
3 answers

Label insertion works with arguments for macros. So try this

 #define VERSION XY123 #define PASTE(x) MyApplication_ ## x #define PRODUCT PASTE(VERSION) 
+7
source share

The ## operator is valid until accepting a placeholder argument. The classic solution is to use a helper:

 #define CONCAT2(a, b) a ## b #define CONCAT(a, b) CONCAT2(a, b) CONCAT(MyApplication_, VERSION) 
+3
source share

All problems in computer science can be solved by an additional level of indirection:

 #define JOIN_(X,Y) X##Y #define JOIN(X,Y) JOIN_(X,Y) #define VERSION XY123 #define PRODUCT JOIN(MyApplication_,VERSION) 
+1
source share

All Articles