宏中参数前面有 # 标记
#define LINK_ENTITY_TO_CLASS(mapClassName,DLLClassName) \
static CEntityFactory<DLLClassName> mapClassName( #mapClassName );
这是来自 Half-Life 2 Alien Swarm mod 的宏,旨在使用 MSVC 进行编译。
我以前从未在宏中见过以 #
开头的参数,并且我不确定这是否是 MSVC 特有的东西或只是不常见。这是什么意思?
#define LINK_ENTITY_TO_CLASS(mapClassName,DLLClassName) \
static CEntityFactory<DLLClassName> mapClassName( #mapClassName );
This is a macro from the Alien Swarm mod for Half-Life 2, meant to be compiled with MSVC.
I've never seen an argument preceded by a #
in a macro before, and I'm not sure if this is a MSVC specific thing or just uncommon. What does it mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是标准 C 和 C++ 的一部分,并且不是特定于实现的。
#
预处理运算符将其参数字符串化。它采用传入宏的任何标记作为其操作数指定的参数(在本例中为参数mapClassName
),并从中生成字符串文字。因此,举一个简单的例子,请注意,参数标记在字符串化之前不会被宏替换,因此如果
Hello
或World
被定义为一个宏,结果仍然是一样的。您需要使用额外的间接级别来替换参数宏(该链接的答案讨论了连接运算符##
,但同样适用于字符串化运算符。This is part of both standard C and C++ and is not implementation-specific. The
#
preprocessing operator stringizes its argument. It takes whatever tokens were passed into the macro for the parameter designated by its operand (in this case, the parametermapClassName
) and makes a string literal out of them. So, for a simple example,Note that the argument tokens are not macro replaced before they are stringized, so if
Hello
orWorld
were defined as a macro, the result would still be the same. You need to use an extra level of indirection to get the arguments macro replaced (that linked answer discusses the concatenation operator,##
, but applies equally to the stringization operator.