views:

59

answers:

2

Hi

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. Does anyone know how to get the space out?

Thanks.

+1  A: 

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...

ShinTakezou
it could be, but the OP specified GNU preprocessor, and I've tested with it
ShinTakezou
@Shin: Sorry; my mistake. C99 added support for empty arguments.
James McNellis
+2  A: 

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);}
James McNellis
It's called token pasting rather that concatenation - string concatenation occurs after tokenisation, pasting before, and doesn't require any operator.
Pete Kirkham
@Pete: Both the C and C++ language standards use the word _concatenate;_ that doesn't mean that _token concatenation_ is the same thing as _string literal concatenation_ (or any other form of concatenation, for that matter).
James McNellis