##(双散列)在预处理器指令中做什么? [英] What does ## (double hash) do in a preprocessor directive?

查看:173
本文介绍了##(双散列)在预处理器指令中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #define DEFINE_STAT(Stat)\ 
struct FThreadSafeStaticStat< FStat _ ## Stat> StatPtr _ ## Stat;

上面这行是从虚幻4,我知道我可以在虚幻的论坛,但我认为这是一个一般的C ++问题,值得被问到这里。



我理解第一行定义一个宏,但是我不熟悉预处理器shenanigans C ++,所以我迷失在那里。逻辑告诉我反斜杠意味着声明继续到下一行。



FThreadSafeStaticStat看起来有点像一个模板,但有#在那里和一个语法I在C ++中从未见过



有人能告诉我这是什么意思吗?我知道你可能无法访问Unreal 4,但它只是我不懂的语法。

解决方案

<$



所以如果你使用



DEFINE_STAT(foo)



在代码中的任何位置,它将替换为



struct FThreadSafeStaticStat< FStat_foo>



这里是另一个例子: 我的博客帖子以进一步解释此问题。

  #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)

begin()
{
printf(Stumped?\\\
);
}

此程序将成功编译并执行,并生成以下输出: p>

  Stumped? 

当预处理器处理此代码时,



< (a,n,i,m,a,t,e)替换为

  • begin() ()

  • decode(a,n,i,m,a,t,e)()替换为 m ## a ## i ## n()

  • m ## a ## i ## n()替换为 main()



有效地, begin()替换为 main()


#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.

解决方案

## 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)

begin()
{
    printf("Stumped?\n");
}

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

Stumped?

When the preprocessor works 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().

这篇关于##(双散列)在预处理器指令中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆