Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining two #defined symbols in C++ preprocessor

I want to do:

#define VERSION XY123
#define PRODUCT MyApplication_VERSION

so that PRODUCT is actually MyApplication_XY123. I have tried playing 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

Is what I want possible?

like image 371
Mr. Boy Avatar asked May 16 '13 15:05

Mr. Boy


People also ask

What is combining two things called?

Some common synonyms of combine are associate, connect, join, link, relate, and unite. While all these words mean "to bring or come together into some manner of union," combine implies some merging or mingling with corresponding loss of identity of each unit.

What does it mean to combine two things?

To combine means to join two or more things together into a single unit. When things are combined, they form combinations. Less commonly, combine can also be used as a noun to refer to several different things, especially a grain harvester and an event at which athletes showcase their skills.

How do I merge two merge?

To choose the merge option, click the arrow next to the Merge button and select the desired merge option. Once complete, the files are merged. If there are multiple files you want to merge at once, you can select multiple files by holding down the Ctrl and selecting each file you want to merge.

What is the antonym of merge '?

Opposite of to combine or blend together to form one substance or mass. separate. split. divide. unmix.


2 Answers

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

#define JOIN_(X,Y) X##Y
#define JOIN(X,Y) JOIN_(X,Y)
#define VERSION XY123
#define PRODUCT JOIN(MyApplication_,VERSION)
like image 200
Jonathan Wakely Avatar answered Oct 18 '22 16:10

Jonathan Wakely


The ## operator acts before argument substitution has taken place. The classical solution is to use a helper:

#define CONCAT2(a, b) a ## b
#define CONCAT(a, b) CONCAT2(a, b)

CONCAT(MyApplication_, VERSION)
like image 32
James Kanze Avatar answered Oct 18 '22 16:10

James Kanze