C++ ##(双哈希)在预处理器指令中做什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22975073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does ## (double hash) do in a preprocessor directive?
提问by DavidColson
#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.
上面的内容取自 Unreal 4,我知道我可以在虚幻论坛上提问,但我认为这是一个通用的 C++ 问题,值得在这里提问。
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.
我知道第一行定义了一个宏,但是我不熟悉 C++ 中的预处理器恶作剧,所以我在那里迷路了。逻辑告诉我反斜杠意味着声明继续到下一行。
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++
FThreadSafeStaticStat 看起来有点像模板,但里面有 # 和我以前从未在 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.
有人能告诉我这是什么意思吗?我知道您可能无法访问 Unreal 4,但这只是我不理解的语法。
回答by Susam Pal
##
is the preprocessor operator for concatenation.
##
是用于连接的预处理器运算符。
So if you use
所以如果你使用
DEFINE_STAT(foo)
DEFINE_STAT(foo)
anywhere in the code, it gets replaced with
在代码中的任何地方,它被替换为
struct FThreadSafeStaticStat<FStat_foo> StatPtr_foo;
struct FThreadSafeStaticStat<FStat_foo> StatPtr_foo;
before your code is compiled.
在你的代码编译之前。
Here is another example from a blog postof 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 withdecode(a,n,i,m,a,t,e)
decode(a,n,i,m,a,t,e)
is replaced withm ## a ## i ## n
m ## a ## i ## n
is replaced withmain
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
Thus effectively, begin()
is replaced with main()
.
因此,有效地,begin()
被替换为main()
。