#define expression explained

gcc 4.4.1

I maintain the code, and I came across something that I don’t understand.

#define RES_API(name, func) name##_##func

Can anyone explain?

Many thanks,

+3
source share
4 answers

The operator ##combines two tokens. In your case, it is nameadded with underline and added with func.

So, RES_API(aName, aFunc)leads to aName_aFunc.

This in itself seems rather annoying. I could see the use of C and C ++ when mixing code, since C libraries tend to prefix their functions, while C ++ libraries put them in a namespace.

Given an alternative definition, for example:

#define RES_API(name, func) name##::##func

You suddenly have a general way of switching between a C interface or C ++.

+4

## - . RES_API(name1, func1) name1_func1. .

+6

I know that you already have your answer, but there is detailed information on the C-FAQ that explains the highlighting of C Preprocessor magic .

+4
source

Instead OBJ_DoSomething, with this macro you can do RES_API(OBJ, DoSomething). Personally, I find him stupid.

+1
source

Source: https://habr.com/ru/post/1725586/


All Articles