Jump to content
• Advertisement

#### Archived

This topic is now archived and is closed to further replies.

# Macro thing...

This topic is 5447 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

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]

#### Share this post

##### Share on other sites
Advertisement
#define FATAL(x...) printf(##x)

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites
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

#### Share this post

##### Share on other sites
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?

#### Share this post

##### Share on other sites
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]

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites
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... --

#### Share this post

##### Share on other sites
quote:
Original post by Javelin
I''m using VC++ 6.0

Which is not even close to a C99 compliant compiler .

#### Share this post

##### Share on other sites
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 .

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5
• Advertisement

• 9
• 33
• 41
• 23
• 13
• ### Forum Statistics

• Total Topics
631745
• Total Posts
3002005
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!