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

后端 未结 1 1455
伪装坚强ぢ
伪装坚强ぢ 2020-12-07 10:34
#define DEFINE_STAT(Stat) \\
struct FThreadSafeStaticStat StatPtr_##Stat;

The above line is take from Unreal 4, and I know I co

相关标签:
1条回答
  • 2020-12-07 11:13

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

    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().

    0 讨论(0)
提交回复
热议问题