Armageddon
Armageddon

Reputation: 381

Concat string with __VA_ARGS__

I've the following macro:

 #define MY_FCT1( id, ... )       \
    FCT( id,__VA_ARGS__ );        \

and I want to create a new one to do something like this:

 #define MY_FCT2( id, ... )                           \
    MY_FCT1( id, (" %s : ",Name())" "__VA_ARGS__);    \

but I get the following error:

error: expression cannot be used as a function

Anyone have an idea how to solve this issue please?

Upvotes: 1

Views: 2648

Answers (1)

luser droog
luser droog

Reputation: 19514

It isn't clear exactly what you're trying to do. A macro cannot call a function. A macro can yield replacement text which includes a function call, but the function won't be called until runtime.

To add Name() to the __VA_ARGS__ which MY_FCT1 receives, just add it like a normal argument with a comma.

 #define MY_FCT2( id, ... )                           \
    MY_FCT1( id, Name(), __VA_ARGS__);

You appear to be trying to use a quoted space character as a concatenation operator. The operator for this (only valid in the replacement text of a macro) is ##. Eg.

#define CAT(x,y) x ## y
//or
#define CAT(x,y) x##y
//                ^ ^    spaces are not relevant here

Upvotes: 1

Related Questions