Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

The above line is take from Unreal 4, and I know I could ask it over on the unreal forums, but I think this is a general C++ question that warrants being asked here.

I understand the first line defines a macro, however I am not well versed in preprocessor shenanigans in C++ and so I'm lost over there. Logic tells me the backslash means the declaration continues onto the next line.

FThreadSafeStaticStat looks a bit like a template, but there's #'s going on in there and a syntax I've never seen before in C++

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
479 views
Welcome To Ask or Share your Answers For Others

1 Answer

## is the preprocessor operator for concatenation.

So if you use

DEFINE_STAT(foo)

anywhere in the code, it gets replaced with

struct FThreadSafeStaticStat<FStat_foo> StatPtr_foo;

before your code is compiled.

Here is another example from a blog post of mine to explain this further.

#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?
");
}

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

Stumped?

When the preprocessor is invoked on this code,

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

Thus effectively, begin() is replaced with main().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...