Macro question

J

John Blevin

I can play games with macro names, to do something like:

#define MACRO_TYPE_FOO(arg) { some_function(arg); }
#define MACRO_TYPE(arg1, arg2) MACRO_TYPE_##arg1(arg2)

MACRO_TYPE(FOO, 5);

But if I try to define a macro to represent the arguments, it doesn't
work:

#define my_args FOO, 5

MACRO_TYPE(my_args);

In this case it first evaluates the MACRO_TYPE and forms
a MACRO_TYPEmy_args (which of course is not defined), it does this
before expanding the my_args. Do you know if there is a way
to get it to expand the my_args before the MACRO_TYPE?

Thanks!

- John
 
D

Dave Thompson

I can play games with macro names, to do something like:

#define MACRO_TYPE_FOO(arg) { some_function(arg); }
#define MACRO_TYPE(arg1, arg2) MACRO_TYPE_##arg1(arg2)

MACRO_TYPE(FOO, 5);

But if I try to define a macro to represent the arguments, it doesn't
work:

#define my_args FOO, 5

MACRO_TYPE(my_args);

In this case it first evaluates the MACRO_TYPE and forms
a MACRO_TYPEmy_args (which of course is not defined), it does this
before expanding the my_args. Do you know if there is a way
to get it to expand the my_args before the MACRO_TYPE?
#define INVOKE_MACRO_TYPE(args) MACRO_TYPE(args)
INVOKE_MACRO_TYPE(my_args);

at least in gcc-2.95; I'm *pretty* sure it's standard, but this is one
of the more arcane areas of the standard and ICBW.

- David.Thompson1 at worldnet.att.net
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top