Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ## in a #define mean?

What does this line mean? Especially, what does ## mean?

 #define ANALYZE(variable, flag)     ((Something.##variable) & (flag)) 

Edit:

A little bit confused still. What will the result be without ##?

like image 804
Dante May Code Avatar asked Jun 28 '11 08:06

Dante May Code


2 Answers

A little bit confused still. What will the result be without ##?

Usually you won't notice any difference. But there is a difference. Suppose that Something is of type:

struct X { int x; }; X Something; 

And look at:

int X::*p = &X::x; ANALYZE(x, flag) ANALYZE(*p, flag) 

Without token concatenation operator ##, it expands to:

#define ANALYZE(variable, flag)     ((Something.variable) & (flag))  ((Something. x) & (flag)) ((Something. *p) & (flag)) // . and * are not concatenated to one token. syntax error! 

With token concatenation it expands to:

#define ANALYZE(variable, flag)     ((Something.##variable) & (flag))  ((Something.x) & (flag)) ((Something.*p) & (flag)) // .* is a newly generated token, now it works! 

It's important to remember that the preprocessor operates on preprocessor tokens, not on text. So if you want to concatenate two tokens, you must explicitly say it.

like image 138
Yakov Galka Avatar answered Oct 02 '22 16:10

Yakov Galka


## is called token concatenation, used to concatenate two tokens in a macro invocation.

See this:

  • Macro Concatenation with the ## Operator
like image 42
Nawaz Avatar answered Oct 02 '22 17:10

Nawaz