简体   繁体   English

内联方法名称的GCC预处理器

[英]GCC Preprocessor for inline method name

I'm working on a project where I have code like the following: 我正在一个项目中,我的代码如下:

#define NAME() Array

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. 在“数组”和“ _init”之间有一个空格,因为这是一个函数名,所以我显然不需要空格。 Does anyone know how to get the space out? 有人知道如何利用空间吗?

The only way to combine two tokens into one (eg, to combine the result of invoking NAME() and _init ) is to use the concatenation operator ( ## ). 将两个标记合并为一个的唯一方法(例如,将调用NAME()_init的结果合并)的唯一方法是使用串联运算符( ## )。 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. 编辑 :至少它与GNU cpp一起使用。

EDIT2 : tried also with -ansi -pedantic and it seemed to work without warning... EDIT2 :也尝试过-ansi -pedantic ,它似乎在没有警告的情况下工作...

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

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