iamacomputer
iamacomputer

Reputation: 565

macro expansion to additional macro arguments

I'm looking for a way of making a macro that expands to extra arguments:

int constant1=2, constant2=3;
#define add_three_arguments(x,y,z) x+y+z
#define extra_arguments ,constant1,constant2
#define make_value(A) add_three_arguments(A extra_arguments)

int r = make_value(5);

Upvotes: 0

Views: 63

Answers (2)

iamacomputer
iamacomputer

Reputation: 565

The closest solution I have found is this:

int constant1=2, constant2=3;
#define _add_three_arguments(x,y,z) x+y+z
#define add_three_arguments(...) _add_three_arguments(__VA_ARGS__)

#define extra_arguments ,constant1,constant2
#define make_value(A) add_three_arguments(A extra_arguments)

int r = make_value(5);

Which is of course not a solution to the problem I stated. So the current answer seems to be, "this is not possible." But maybe a new version of clang/gcc will somehow enable this. I will leave the question open.

Upvotes: 1

Marian
Marian

Reputation: 7472

Try the following:

#define EMPTY
#define EVAL(X) X
#define add_three_arguments(x,y,z) x+y+z
#define extra_arguments ,constant1,constant2
#define make_value(A) EVAL(add_three_arguments EMPTY (A extra_arguments))

make_value(5);

However, if you redesign your macros you may get a nicer solution not requiring such constructs.

Upvotes: 0

Related Questions