preprocessor and parenthesis

E

effbiae

hi,

this code:

#define M(a,b) static int is[3]={a,b;
M(1,(2,3)})

produces this (using gcc -E):

static int is[3]={1,(2,3)};

which gcc does not complain about.

my question(s):
o is there a better way of passing a comma separated list as the 'b'
parameter
o is gcc being generous by
a) preprocessing this
b) compiling it
o how can i write a macro like this:

#define F(a,b) int a(obj*o b

so that i can write function declarations in which all functions have a
first param obj, but may optionally have more.

thanks,


jack
ivorykite.com
 
G

Guest

effbiae said:
hi,
Hi,

this code:

#define M(a,b) static int is[3]={a,b;
M(1,(2,3)})

How about moving the } to the macro definition?

#define M(a,b) static int is[3]={a,b};
M(1,(2,3))

Isn't that more readable?
produces this (using gcc -E):

static int is[3]={1,(2,3)};

which gcc does not complain about.

gcc 3.4 and 4 do complain about that no matter what, and older versions
complain about it with -pedantic. Additionally, on those older
versions, it probably doesn't do what you expect: it initialises is[0]
with 1, and is[1] with (2,3), which evaluates to 3.
my question(s):
o is there a better way of passing a comma separated list as the 'b'
parameter

Variadic macros.

#define M(...) static int is[3]={__VA_ARGS__};
M(1,2,3)

Or, if you want to enforce a minimum of two arguments:

#define M(a, ...) static int is[3]={a, __VA_ARGS__};
M(1,2,3)
o is gcc being generous by
a) preprocessing this
Nope.

b) compiling it
Yep.

o how can i write a macro like this:

#define F(a,b) int a(obj*o b

so that i can write function declarations in which all functions have a
first param obj, but may optionally have more.

Again, why not put the ) in the macro definition?

#define F(a,b) int a(obj*o b)

gcc has a non-standard extension that makes this easy -- refer to the
documentation if you're interested -- but with standard C it, while
possible, is needlessly complex since you don't know in advance whether
you'll need a comma after o. The easiest route is to create two macros:

#define F_0(a) int a(obj* o)
#define F_1(a,...) int a(obj* o, __VA_ARGS__)

and choose the appropriate one by hand each time.

HTH
 
E

effbiae

Harald wrote:

thanks for your help.
Variadic macros.

of course...
you'll need a comma after o. The easiest route is to create two macros:

#define F_0(a) int a(obj* o)
#define F_1(a,...) int a(obj* o, __VA_ARGS__)

and choose the appropriate one by hand each time.

i think i could do something like this:
#define F(a) int a(obj*o,...)
and leave the argument-getting up to the function

do i pay an efficiency penalty for using va_list macros/functions?

thanks,



jack
 
G

Guest

effbiae said:
i think i could do something like this:
#define F(a) int a(obj*o,...)
and leave the argument-getting up to the function

Yeah, that should work. You may now accidentally call the functions
with the wrong number of arguments, of course, so be careful about
that.
do i pay an efficiency penalty for using va_list macros/functions?

In theory, yes, but it's nothing likely to be noticeable. If it makes
things easier, go for it.
 
T

tmp123

effbiae said:
hi,

this code:

#define M(a,b) static int is[3]={a,b;
M(1,(2,3)})

produces this (using gcc -E):

static int is[3]={1,(2,3)};

which gcc does not complain about.

my question(s):
o is there a better way of passing a comma separated list as the 'b'
parameter
o is gcc being generous by
a) preprocessing this
b) compiling it
o how can i write a macro like this:

#define F(a,b) int a(obj*o b

so that i can write function declarations in which all functions have a
first param obj, but may optionally have more.

thanks,


jack
ivorykite.com

Hello,

If all the others posibilities fails, you can always use an external
and more powerful preprocessor, like "m4".

Kind regards.
 
J

Jordan Abel

hi,

this code:

#define M(a,b) static int is[3]={a,b;
M(1,(2,3)})

produces this (using gcc -E):

static int is[3]={1,(2,3)};

which gcc does not complain about.

I believe this statement is equivalent to
static int is[3] = {1,3,0};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top