Sign in to follow this  

#define macro with variable args

This topic is 4863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there any way, in C, to define a macro that takes a variable amount of arguments? Something like
#define mac(func,args)   \       
func(args);


(only not)? I'm getting no from everyone I ask but perhaps someone here can bend the laws of the universe.

Share this post


Link to post
Share on other sites
Variadic macros are not standard. Check your preprocessor's documentation to see it it supports them. (AFAIK, gcc does, MSVC doesn't).

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Variadic macros are not standard.

Oh, ok. That pretty much somes it up as I can't write something specific to my compiler. Thanks a lot. (I'll look up "Variadic macros" out of interest in any event out of curiosity.)

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Variadic macros are not standard. Check your preprocessor's documentation to see it it supports them. (AFAIK, gcc does, MSVC doesn't).

If I recall correctly, they are a standard feature of C99. I guess it depends which C he's talking about...

Share this post


Link to post
Share on other sites
Quote:
Original post by Null and Void
Quote:
Original post by Fruny
Variadic macros are not standard. Check your preprocessor's documentation to see it it supports them. (AFAIK, gcc does, MSVC doesn't).

If I recall correctly, they are a standard feature of C99. I guess it depends which C he's talking about...

I'm writing in VS.net 2003 at present. However, that's not the only implementation, nor the only platform that this may be used for. With the actual terminology at my disposal a quick google would appear indicate that they are in fact not implemented in VS.

Thanks again.

Share this post


Link to post
Share on other sites
Hi,

Well, I think macros are evil any way, but still I believe you can do what you are trying to do there without any variadic macro stuff. It's really easy actually:


#define MAC(func, args) func args; /* Note no parens */

void macro_test() {
/* A function with no args: */
MAC(noargfunc, ());

/* A function with one arg: */
MAC(oneargfunc, (foo));

/* A function with many args: */
MAC(manyargfunc, (foo, bar, baz));

/* A function with *gasp* variable args */
MAC(printf, ("Hello, %s\n", foo));
}




Then, if we run that source through the pre-processor, then what we get is this:


# 1 "test.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "test.c"



void macro_test() {

noargfunc ();;


oneargfunc (foo);;


manyargfunc (foo, bar, baz);;


printf ("Hello, %s\n", foo);;
}




(I did that with gcc.)

Note of course that I accidentally put a semi-colon in the macro definition, and after macro calls, so there are two after each function call, but whatever. It'll work. ;)

Note also, that here's where the fact that macros is evil comes in. If you don't call the macro like this: MAC(name, (params)) - but do like this: MAC(name, params) - then everything will go to hell, and you will be beating your head against the wall trying to debug that. ;)

Vovan

Share this post


Link to post
Share on other sites
vovansim, I think you've done it. It's what I had for one of my test, save for the lack of parentheses in the macro definition of
func args;

Superb.

I'm still hampered in another case with the lack of true variable amount of params, but that solves one instance.

Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by Null and Void
If I recall correctly, they are a standard feature of C99. I guess it depends which C he's talking about...


You recall correctly:


#define DEBUGF(f,...) (fprintf(dbgf, "%s(): ", f), fprintf(dbgf, __VA_ARGS__))

Share this post


Link to post
Share on other sites

This topic is 4863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this