宏中的波形符 (~) 是什么意思?
在此网站上看到的代码显示了宏调用在括号中使用波形符:
HAS_COMMA(_TRIGGER_PARENTHESIS_ __VA_ARGS__ (~))
// ^^^
这是什么意思/做什么?我怀疑这只是一个空洞的论点,但我不确定。它是否可能特定于 C(99),就像 __VA_ARGS__
特定于 C99 并且存在于 C++ 中?
Seen on this site, the code shows macro invocations using a tilde in parentheses:
HAS_COMMA(_TRIGGER_PARENTHESIS_ __VA_ARGS__ (~))
// ^^^
What does it mean / do? I suspect it to just be an empty argument, but I'm not sure. Is it maybe specific to C(99) like the __VA_ARGS__
is specific to C99 and existent in C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在Boost.Preprocessor的介绍页面,在A.4.1.1 Horizontal Repetition中给出了一个例子,
解释如下:
(强调我的)
并且有注释:
因此,波形符只是一个占位符,因为需要参数,但没有任何参数是必需的。由于任何想要扩展的用户定义标识符都可以扩展,因此您需要使用其他东西。
事实证明,与
+
或-
相比,~
几乎没有被使用(二进制否定并不经常被调用),所以有混淆的可能性很小。一旦您确定了这一点,持续使用它就会给波形符带来新含义;就像使用operator<<
和operator>>
来流数据一样,已经成为 C++ 的习惯用法。On the introduction page of Boost.Preprocessor, an example is given in A.4.1.1 Horizontal Repetition
An explanation is provided below:
(emphasis mine)
And there is the note:
The tilde, therefore, is simply a place holder because an argument is required, but none is necessary. Since any user-defined identifier wannabe could be expanded, you need to use something else.
It turns out that
~
is pretty much unused (binary negation is not that often called) in comparison to+
or-
for example, so there is little chance of confusion. Once you've settled on this, using it consistently gives it a new meaning to the tilde; like usingoperator<<
andoperator>>
for streaming data has become a C++ idiom.~
不执行任何操作。括号内的几乎所有其他内容都具有相同的作用。此技巧的关键是测试
_TRIGGER_PARENTHESIS_
是否位于_TRIGGER_PARENTHESIS_ __VA_ARGS__ (~)
扩展中的(~)
旁边。无论哪种方式,HAS_COMMA(...)
都会将其参数扩展为0
或1
。The
~
does nothing. Almost any other content inside those parentheses would work the same.The lynchpin of this trick is to test whether
_TRIGGER_PARENTHESIS_
is next to(~)
in the expansion of_TRIGGER_PARENTHESIS_ __VA_ARGS__ (~)
. Either way,HAS_COMMA(...)
expands its arguments to either0
or1
.要测试的参数放置在宏及其括号之间,只有当参数为空时宏才会触发:
注意:实际上您发布的链接说明了这一点。我将检查标准中对此的引用。
The arguments to be tested is placed between the macro and its parenthesis, the macro only triggers if the arguments are empty:
NOTE: Actually the very link you posted states it. I will check for a reference to this in the standard.