#define mac(func,args) \
func(args);
#define macro with variable args
Is there any way, in C, to define a macro that takes a variable amount of arguments? Something like
(only not)?
I'm getting no from everyone I ask but perhaps someone here can bend the laws of the universe.
Variadic macros are not standard. Check your preprocessor's documentation to see it it supports them. (AFAIK, gcc does, MSVC doesn't).
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.)
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...
Quote:Original post by Null and VoidQuote: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.
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:
Then, if we run that source through the pre-processor, then what we get is this:
(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
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
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
Superb.
I'm still hampered in another case with the lack of true variable amount of params, but that solves one instance.
Thanks.
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.
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__))
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement