带有来自第一个宏的参数的宏到另一个宏

发布于 2024-10-09 01:27:06 字数 317 浏览 1 评论 0原文

我正在尝试使这个宏起作用:

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

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

DefineMethodsLenght.Mode(caller, Lenght) 

而不是

DefineMethods(Lenght.Mode, caller, Lenght);

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

I'm trying to make this macro work:

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

Now this doesn't seem to be working this gets me:

DefineMethodsLenght.Mode(caller, Lenght) 

instead of

DefineMethods(Lenght.Mode, caller, Lenght);

What's going on here; isn't this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧城空念 2024-10-16 01:27:06

好吧,您的问题标题暗示涉及两个宏,但您的示例仅显示一个宏。所以我会继续猜测你有:

#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 宏中,但由于您没有在问题中显示它,所以我们无法真正判断发生了什么。

Well, the title of your question implies that there are TWO macros involved, but your example only shows one macro. So I'll go ahead and guess that you have:

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

Now if you invoke this with

InitProperty(Lenght, caller)

you will get (after macro expansion)

DefineMethodsLenght.Mode(caller, Lenght)

which is what you describe. You say you want

DefineMethods(Lenght.Mode, caller, Lenght)

which does exist midway through the macro expansion process, but after expanding the DefineMethods macro you'll get what you see. If that's NOT what you want, the problem is in the DefineMethods macro and not the InitProperty macro, but as you don't show it in your question, we can't really tell what is going on.

薔薇婲 2024-10-16 01:27:06

如果您想要您所说的结果,那么您不需要使用任何预处理运算符;您只需要:

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

## 运算符用于连接标记。鉴于您当前的宏定义,我不明白为什么您会得到您所说的结果,但您对 ## 的使用绝对是不正确的。串联的结果要求是单个预处理标记,并且 (Lenght 是两个预处理标记 --(Lenght --not一。

If you want the result that you say you want, then you don't need to use any preprocessing operators; you just need:

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

The ## operator is used to concatenate tokens. Given your current macro definition, I don't see why you'd get the result you say you get, but your use of ## is definitely incorrect. The result of the concatenation is required to be a single preprocessing token, and (Lenght is two preprocessing tokens--( and Lenght--not one.

深者入戏 2024-10-16 01:27:06

我想

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

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

I think

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

should be correct.
But to be safer, it's better to use:

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

If the problem remains, why not try this?

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

InitProperty(Length, caller);

If "Name", "Type" are not also macros.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文