开发者

Macro to an other macro with arguments from first macro

开发者 https://www.devze.com 2023-02-01 19:19 出处:网络
I\'m trying to make this macro work: #define InitProperty(Name, Type) DefineMethods ( ##Name.Mode, Type, Name )

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:

DefineMe开发者_JS百科thodsLenght.Mode(caller, Lenght) 

instead of

DefineMethods(Lenght.Mode, caller, Lenght);

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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号