What does ## (double hash) do in the preprocessor directive?

#define DEFINE_STAT(Stat) \ struct FThreadSafeStaticStat<FStat_##Stat> StatPtr_##Stat; 

The above line is taken from Unreal 4, and I know that I can talk about this in unrealistic forums, but I think this is a general C ++ question that requires to be asked.

I understand that the first line defines the macro, however I am not very versed in prefabricated shenanigans in C ++, and therefore I got lost there. Logic tells me that the backslash means that the declaration continues on the next line.

FThreadSafeStaticStat is a bit like a template, but there # happens there and syntax that I have never seen before in C ++

Can someone tell me what that means? I understand that you may not have access to Unreal 4, but this is just syntax that I don't understand.

+77
c ++ c c-preprocessor concatenation
Apr 09 '14 at 22:21
source share
1 answer

## is the preprocessor operator for concatenation.

So if you use

DEFINE_STAT(foo)

anywhere in the code it is replaced by

struct FThreadSafeStaticStat<FStat_foo> StatPtr_foo;

before your code is compiled.

Here is another example from my blog to explain this in more detail.

 #include <stdio.h> #define decode(s,t,u,m,p,e,d) m ## s ## u ## t #define begin decode(a,n,i,m,a,t,e) int begin() { printf("Stumped?\n"); } 

This program will compile and execute successfully, and will produce the following output:

 Stumped? 

When the preprocessor is called for this code,

  • begin is replaced by decode(a,n,i,m,a,t,e)
  • decode(a,n,i,m,a,t,e) is replaced by m ## a ## i ## n
  • m ## a ## i ## n is replaced by main

Thus, begin() actually replaced with main() .

+150
Apr 09 '14 at 22:33
source share



All Articles