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 ##
?
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 ##
?
##
is called token concatenation, used to concatenate two tokens in a macro invocation.See this:
One very important part is that this token concatenation follows some very special rules:
e.g. IBM doc:
Examples are also very self explaining
With output:
Source is the IBM documentation. May vary with other compilers.
To your line:
It concatenates the variable attribute to the "Something." and adresses a variable which is logically anded which gives as result if Something.variable has a flag set.
So an example to my last comment and your question(compileable with g++):
According to Wikipedia
Check Token Concatenation
This is not an answer to your question, just a CW post with some tips to help you explore the preprocessor yourself.
The preprocessing step is actually performed prior to any actual code being compiled. In other words, when the compiler starts building your code, no #define statements or anything like that is left.
A good way to understand what the preprocessor does to your code is to get hold of the preprocessed output and look at it.
This is how to do it for Windows:
Create a simple file called test.cpp and put it in a folder, say c:\temp. Mine looks like this:
Not very useful, but simple. Open the Visual studio command prompt, navigate to the folder and run the following commandline:
So, it's the compiler your running (cl.exe), with your file, and the /P option tells the compiler to store the preprocessed output to a file.
Now in the folder next to test.cpp you'll find test.i, which for me looks like this:
As you can see, no #define left, only the code it expanded into.
Usually you won't notice any difference. But there is a difference. Suppose that
Something
is of type:And look at:
Without token concatenation operator
##
, it expands to:With token concatenation it expands to:
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.
lets consider a different example:
consider
without the
##
, clearly the preprocessor cant seex
andy
as separate tokens, can it?In your example,
##
is simply not needed as you are not making any new identifier. In fact, compiler issues "error: pasting "." and "variable" does not give a valid preprocessing token"