Macro thing...

Started by
13 comments, last by Javelin 20 years, 8 months ago
Hi Is it possible in C/C++ to make a macro with unspecified number of parameters? E.g. calling printf() with a macro. #define PRINTF(x,...) printf(x,...) Surely not like this but I think you catch my drift // Javelin -- Why do something today when you can do it tomorrow... -- [edited by - Javelin on August 18, 2003 7:01:27 PM]
// Javelin// Assumption is the mother of all fuckups...
Advertisement
#define FATAL(x...) printf(##x)
Basically, no. Macros are text expansions. If you used your example macro in some code it would cause an error.

Remember, macros will replace any argument in their definition with the text as you use them. ''...'' is not a valid symbol as I recall (although it''s a bit late to be getting the text books out).

Macros will tokenise based upon the commas, so you can''t expect a list longer than the macro to be expanded.

It sounds like you''re attempting to default the first few parameters of a variable argument list function. You mentioned C++ as well as C so I would suggest that if you''re using C you write a wrapper function or find a way to avoid using expensive var arg functions, and if you''re using C++ then I would suggest you examine your design carefully ;-)

If you''re just after saving a bit of typing, it''s probably not worth it.
In C: #define PRINTF(...) printf(__VA_ARGS__)
In C++, I''m not aware of any standard way. However, there''s another way to get the same effect for this example: #define PRINTF printf
quote:Original post by Javelin
Hi

Is it possible in C/C++ to make a macro with unspecified number of parameters? E.g. calling printf() with a macro.

Why do you need this?
Hi again...

C-Junkie, Beer Hunter
I tested your examples, but they did not appear to work (in C as well as C++).


quote:
Why do you need this?


Well... I'm rebuilding my TRACE/DEBUG functions and I use macros to be able to turn on/off them in an easy way. The problem is that I really want a printf syntax when writing the messages.

E.g.

#ifdef _NODEBUG_  #define DEBUGTXT(str)#else  #define DEBUGTXT(str) debug.Write(str);#endif


I can solve it the way I did before, by having several DEBUGTXT taking diferent nub´mber of parameters. This is not what I want though.



// Javelin
-- Why do something today when you can do it tomorrow... --

[edited by - Javelin on August 19, 2003 7:24:23 AM]
// Javelin// Assumption is the mother of all fuckups...
quote:Original post by Javelin
I tested your examples, but they did not appear to work (in C as well as C++).
__VA_ARGS__ was introduced in the C99 standard. Make sure you''re using a C99-compliant compiler. With my C++ example, you must''ve changed something. Show me what you tried.
quote:
__VA_ARGS__ was introduced in the C99 standard. Make sure you''re using a C99-compliant compiler. With my C++ example, you must''ve changed something. Show me what you tried.


It''s not the __VA_ARGS__ that do not work but the ... in the macro. I tested it exactly as you pasted it. Copy/paste...

I''m using VC++ 6.0



// Javelin
-- Why do something today when you can do it tomorrow... --
// Javelin// Assumption is the mother of all fuckups...
quote:Original post by Javelin
I''m using VC++ 6.0

Which is not even close to a C99 compliant compiler .

Oh, and for C++: I used a trick like this once for what''s not really a variable argument macro, but it might be useful for you:
#include <sstream>#define MACRO(alpha, beta, list) {    \  std::ostringstream sstr;            \  sstr << list;                       \  some_func(alpha, beta, sstr.str()); \}MACRO(123, 456, "integer: " << 7 << ", char: " << ''a'');

Of course, you could do it with all sorts of operator overloads. Should you? It depends upon the situation .

This topic is closed to new replies.

Advertisement