开发者

GCC Preprocessor for inline method name

开发者 https://www.devze.com 2023-01-03 11:10 出处:网络
I\'m working on a project where I have code like the following: #define NAME() Array开发者_C百科 inline NAME()* NAME()_init (void* arg0){return (NAME()*)Object_init(arg0);}

I'm working on a project where I have code like the following:

#define NAME() Array开发者_C百科

inline NAME()* NAME()_init (void* arg0){return (NAME()*)Object_init(arg0);}

But I get the following result:

inline Array* Array _init (void* arg0){return (Array*)Object_init(arg0);}

With a space between the "Array" and the "_init" Because this is a function name, I obviously do not want the space. Does anyone know how to get the space out?


The only way to combine two tokens into one (e.g., to combine the result of invoking NAME() and _init) is to use the concatenation operator (##). You'll need to do something like so:

#define REAL_CONCATENATE(x, y) x ## y
#define CONCATENATE(x, y) REAL_CONCATENATE(x, y)

#define NAME() Array
inline NAME()* CONCATENATE(NAME(), _init) (void* arg0){return (NAME()*)Object_init(arg0);}

Yes, the extra level of indirection is necessary.

Note that you don't need to use a function-like macro if you take no parameters, so you could just as easily use:

#define NAME Array
inline NAME* CONCATENATE(NAME, _init) (void* arg0){return (NAME*)Object_init(arg0);}


You should change the semantics in something like this:

#define NAME(X) Array##X
inline NAME()* NAME(_init) (void* arg0){return (NAME()*)Object_init(arg0);}

EDIT: At least it works with GNU cpp.

EDIT2: tried also with -ansi -pedantic and it seemed to work without warning...

0

精彩评论

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

关注公众号