C++ macro #39;###39; doesn#39;t work after #39;-gt;#39; operator(C++ 宏 ## 在 -gt; 之后不起作用操作员)
问题描述
我有一个shared_ptr对象x,它有如下get和set方法:
I have a shared_ptr object x, which has get and set methods as follows:
当我尝试定义宏时:
它工作正常,但是当我这样做时:
It works fine, but when I do:
它给出以下编译错误:粘贴形成的'->a',一个无效的预处理令牌
推荐答案
预处理器处理令牌" - 喜欢名称和运算符.
The preprocessor works on "tokens" - likes names and operators.
##
运算符通过将较小的部分粘贴在一起来创建新标记.在第一个示例中,set_##type##_value
变为 set_a_value
,这是一个有效的令牌.
The ##
operator creates a new token by pasting smaller parts together. In the first example set_##type##_value
becomes set_a_value
, which is a valid token.
在第二个示例中,->##type##_value
将变为 ->a_value
,这不是有效的预处理器令牌.应该是两个token.
In the second example ->##type##_value
would become ->a_value
, which is not a valid preprocessor token. It ought to be two tokens.
如果您只是将行 x->type##_value();
它应该可以工作.你会得到单独的标记 x
, ->
, a_value
, (
, )
, 和 ;
.
If you just make the line x->type##_value();
it should work. You get the separate tokens x
, ->
, a_value
, (
, )
, and ;
.
这篇关于C++ 宏 '##' 在 '->' 之后不起作用操作员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!