#define DEFINE_STAT(Stat) \
struct FThreadSafeStaticStat<FStat_##Stat> StatPtr_##Stat;
上面的行取自虚幻 4,我知道我可以在虚幻论坛上询问它,但我认为这是一个一般的 C++ 问题,值得在这里提出。
我知道第一行定义了一个宏,但是我不太熟悉 C++ 中的预处理器恶作剧,所以我迷失了方向。逻辑告诉我反斜杠意味着声明继续到下一行。
FThreadSafeStaticStat 看起来有点像模板,但里面有 # 和我以前在 C++ 中从未见过的语法
谁能告诉我这是什么意思?我知道您可能无法访问虚幻 4,但这只是我不理解的语法。
最佳答案
##
是连接的预处理器运算符。
所以如果你使用
DEFINE_STAT(foo)
代码中的任何位置,它都会被替换为
struct FThreadSafeStaticStat<FStat_foo> StatPtr_foo;
在编译代码之前。
这是 a blog post 中的另一个示例我的进一步解释这一点。
#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");
}
该程序将成功编译并执行,并产生以下输出:
Stumped?
当在此代码上调用预处理器时,
-
begin
替换为decode(a,n,i,m,a,t,e)
-
decode(a,n,i,m,a,t,e)
替换为m ## a ## i ## n
-
m ## a ## i ## n
替换为main
因此,begin()
替换为 main()
.
关于c++ - ##(双哈希)在预处理器指令中起什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55563244/