-1
#define DECLARE_DELETE_PTR(type) \
void DeletePtr_##type(string &operand) \
{\
}\

What's the meaning of ## in the macro definition in C++?

what's the difference with the followed code?

#define MAKE_STRINGS(VAR) #VAR

it is only one #, but the former is two #

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
jiafu
  • 6,338
  • 12
  • 49
  • 73
  • See (amongst others) [Macro directives in C](http://stackoverflow.com/questions/8685619/macro-directives-in-c-my-code-example-doesnt-work) for a discussion of stringification (with a single `#`, as mentioned in comments to various answers), and [C preprocessor and concatenation](http://stackoverflow.com/questions/1489932/c-preprocessor-and-concatenation/) for a duplicate of this question. – Jonathan Leffler May 13 '13 at 06:59

3 Answers3

3

It asks the precompiler to concatenate two tokens.

#define DECLARE_DELETE_PTR(type) \
void DeletePtr_##type(string &operand) \
{\
}\

DECLARE_DELETE_PTR(int) would give :

void DeletePtr_int(string &operand)
             //^^^ the macro argument is int, so the precompiler replaces it here
{
}

Indeed, in the macro code, the argument type is concatenated with the rest of the command. If the macro argument is int, then it's just a simple subsitution giving the above result. Remember that since it's a pre-processor directive, it happens entirely at compile time.

If you're on linux, I recommand giving a look at the cpp command to try and understand better.


As for your second question, the difference is that it's simply two different operators.

Just as the name implies -> it turns its argument into a c-string (I've just tried that). For example :

std::cout << MAKE_STRINGS(Hello World) << std::endl;

would turn into:

std::cout << "Hello World" << std::endl

Or, more interesting :

std::cout << MAKE_STRINGS("Hello" World) << std::endl;

becomes:

std::cout << "\"Hello\" World" << std::endl;

It appears it also takes care of escaping special characters, but I could be wrong - that comes from experimentation from 3 minutes ago.

Nbr44
  • 2,042
  • 13
  • 15
2

It concatenates the value you pass through the parameter type...

DECLARE_DELETE_PTR(gremlin)

Would expand to:

void DeletePtr_gremlin(string &operand)
{
}
paddy
  • 60,864
  • 6
  • 61
  • 103
  • what's the difference with the followed code? #define MAKE_STRINGS(VAR) #VAR it is only one #, but the former is two # – jiafu May 13 '13 at 05:22
  • 1
    A single hash does *stringification* (it takes your value and encloses it in double-quotes). The double hash does *substitution*. So, `MAKE_STRINGS(gremlin)` would expand to `"gremlin"`. – paddy May 13 '13 at 05:24
  • @Paddy: No -- the double hash does token concatenation, so (for example) `x##y` is pasted together to become a single token, `xy`. – Jerry Coffin May 13 '13 at 05:27
2

The ## operator is used to concatenate two tokens. Here is an example:

DECLARE_DELETE_PTR(MyType) 

// would expand into

void DeletePtr_MyType(string &operand)
{
}
Arun
  • 2,087
  • 2
  • 20
  • 33
  • what's the difference with the followed code? #define MAKE_STRINGS(VAR) #VAR it is only one #, but the former is two # – – jiafu May 13 '13 at 05:24
  • Using a single "#" results in stringification. Have a look at http://gcc.gnu.org/onlinedocs/cpp/Stringification.html – Arun May 13 '13 at 05:28