繁体   English   中英

宏到另一个宏,带有第一个宏的参数

[英]Macro to an other macro with arguments from first macro

我正在尝试使此宏工作:

#define InitProperty(Name, Type) DefineMethods ( ##Name.Mode, Type, Name )

现在,这似乎不起作用,这让我:

DefineMethodsLenght.Mode(caller, Lenght) 

代替

DefineMethods(Lenght.Mode, caller, Lenght);

这里发生了什么; 这不可能吗?

如果您要获得想要的结果,则无需使用任何预处理运算符。 您只需要:

#define InitProperty(Name, Type) DefineMethods ( Name.Mode, Type, Name )

##运算符用于连接令牌。 给定您当前的宏定义,我不明白为什么会得到您说得到的结果,但是您对##的使用绝对不正确。 级联的结果是必需的是一个单一的预处理标记,和(Lenght是二预处理tokens-- (Lenght --not之一。

好吧,您的问题的标题意味着涉及两个宏,但是您的示例仅显示一个宏。 因此,我继续猜测您有:

#define InitProperty(Name, Type) DefineMethods ( Name.Mode, Type, Name )
#define DefineMethods(Name, Type, Arg)  DefineMethods##Name( Type, Arg )

现在,如果您使用

InitProperty(Lenght, caller)

您将获得(宏扩展后)

DefineMethodsLenght.Mode(caller, Lenght)

这就是你所描述的。 你说你要

DefineMethods(Lenght.Mode, caller, Lenght)

确实存在于宏扩展过程的中间,但是在扩展DefineMethods宏之后,您将获得所看到的内容。 如果这不是您想要的,那么问题出在DefineMethods宏中,而不是InitProperty宏中,但是由于您没有在问题中显示它,因此我们无法真正知道发生了什么。

我认为

#define InitProperty(Name, Type) DefineMethods ( Name.Mode, Type, Name )

应该是正确的。 但是为了安全起见,最好使用:

#define InitProperty(Name, Type) DefineMethods ( (Name).Mode, (Type), (Name) )

如果问题仍然存在,为什么不试试呢?

template<TName, TType>
void InitProperty(TName Name, TType Type) {
    DefineMethods (Name.Mode, Type, Name);
}

InitProperty(Length, caller);

如果“名称”,“类型”也不是宏。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM