Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 511 Vote(s) - 3.41 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What does ## in a #define mean?

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

<pre>
#define ANALYZE(variable, flag) ((Something.##variable) & (flag))
</pre>

Edit:

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

#2
According to Wikipedia

> Token concatenation, also called token pasting, is one of the most subtle — and easy to abuse — features of the C macro preprocessor. Two arguments can be 'glued' together using ## preprocessor operator; this allows two tokens to be concatenated in the preprocessed code. This can be used to construct elaborate macros which act like a crude version of C++ templates.

Check [Token Concatenation](

[To see links please register here]

)
Reply

#3
One very important part is that this token concatenation follows some very special rules:

e.g. IBM doc:

- Concatenation takes place before any
macros in arguments are expanded.
- **If the result of a concatenation is a
valid macro name**, it is available for
further replacement even if it
appears in a context in which it
would not normally be available.
- If **more than one** ## operator and/or #
operator appears in the replacement
list of a macro definition, **the order
of evaluation of the operators is not
defined.**

Examples are also very self explaining

#define ArgArg(x, y) x##y
#define ArgText(x) x##TEXT
#define TextArg(x) TEXT##x
#define TextText TEXT##text
#define Jitter 1
#define bug 2
#define Jitterbug 3

With output:

ArgArg(lady, bug) "ladybug"
ArgText(con) "conTEXT"
TextArg(book) "TEXTbook"
TextText "TEXTtext"
ArgArg(Jitter, bug) 3

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++):

// this one fails with a compiler error
// #define ANALYZE1(variable, flag) ((Something.##variable) & (flag))
// this one will address Something.a (struct)
#define ANALYZE2(variable, flag) ((Something.variable) & (flag))
// this one will be Somethinga (global)
#define ANALYZE3(variable, flag) ((Something##variable) & (flag))
#include <iostream>
using namespace std;

struct something{
int a;
};

int Somethinga = 0;

int main()
{
something Something;
Something.a = 1;

if (ANALYZE2(a,1))
cout << "Something.a is 1" << endl;
if (!ANALYZE3(a,1))
cout << "Somethinga is 0" << endl;
return 1;
};

Reply

#4
> 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.
Reply

#5
lets consider a different example:

consider

#define MYMACRO(x,y) x##y

without the `##`, clearly the preprocessor cant see `x` and `y` as separate tokens, can it?

In your example,

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

`##` 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"
Reply

#6
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:

#define dog_suffix( variable_name ) variable_name##dog

int main()
{
int dog_suffix( my_int ) = 0;
char dog_suffix( my_char ) = 'a';

return 0;
}

Not very useful, but simple. Open the Visual studio command prompt, navigate to the folder and run the following commandline:

c:\temp>cl test.cpp /P

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:

#line 1 "test.cpp"


int main()
{
int my_intdog = 0;
char my_chardog = 'a';

return 0;
}


As you can see, no *#define* left, only the code it expanded into.




Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through