Solved

Macro that can take variable qty of arguments

Posted on 2002-03-25
10
375 Views
Last Modified: 2012-05-04
How can I make a macro that can take variable qty of arguments.

#ifdef DEBUG
#define DEBUG_OUTPUT(Data) print_debug(Data)
#else
#define DEBUG_OUTPUT(Data)
#endif

void function_x(int xyz)
{
 DEBUG_OUTPUT("function_x(%i)", xyz);
}

void Widgit_func(int xyz, string somedat)
{
 DEBUG_OUTPUT("Widgit_func(%i, %s)", xyz, somedat.c_str());
}

I need DEBUG_OUTPUT marco that can take any number of arguments.


0
Comment
Question by:LO19810527
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6894945
If you're using gcc, you can simply

#define DEBUG_OUTPUT(...) print_debug(...)

This won't work with other compilers, though...
0
 
LVL 6

Expert Comment

by:thienpnguyen
ID: 6894954
#ifdef DEBUG
#define DEBUG_OUTPUT MyPrintf
#else
#define DEBUG_OUTPUT EmptyPrint
#endif



#include <stdio.h>
#include <stdarg.h>

void MyPrintf( char *s, ... )
{
}

void MyPrintf( char *s, ... )
{
va_list argList;
char msg[3000];

va_start( argList, s );
vsprintf( msg, s, argList);
va_end( argList );

printf("%s\n", msg );

}

int main()
{

    MyPrintf("%s %d", "Hello", 123);
    return 1;
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 6894956
BTW, just in case you weren't already aware of that: No other compiuler supports the construct of a 'macro taking a variable number of arguments', so you will eventually either end up having to use a function to perform that or use multiple macros that take account of the number of arguments you're passing, e.g. like

#ifdef DEBUG
#define DEBUG_OUTPUT1(d1) print_debug(d1)
#define DEBUG_OUTPUT2(d1, d2) print_debug(d1, d2)
// etc.pp.
#else
//...
#endif
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 6894960
thienpnguyen, your suggestion still involves an 'unnecessary' function call - and, BTW, I'd personally make it read

void MyPrintf( char *s, ... )
{
#ifdef DEBUG
//...
#endif
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 6894963
>>your suggestion still involves an 'unnecessary' function call

Which isn't really too bad, actually :o)
0
 
LVL 30

Accepted Solution

by:
Axter earned 300 total points
ID: 6894968
What compiler are you using?

You could try the following:

#if DEBUG
#ifdef __GNUC__
#define DEBUG_PRINT(args...) printf_debug(args)
#else
#define DEBUG_PRINT printf_debug
#endif //__GNUC__
#else  
#ifdef __GNUC__
#define DEBUG_PRINT(args...)
#else
#define DEBUG_PRINT /##/
#endif //__GNUC__
#endif

The above method should work for both VC and g++
0
 
LVL 30

Expert Comment

by:Axter
ID: 6894982
FYI:
The VC method does have one bad side effect.
Example:

//This works OK
void function_x(int xyz)
{
  DEBUG_PRINT("function_x(%i)", xyz);
  printf("Hello World");
}

//This will NOT works OK
void function_x(int xyz)
{
  if (xyz == 123) DEBUG_PRINT("function_x(%i)", xyz);
  printf("Hello World");
}

In NON-Debug mode, the compiler will see the second code like this:
if (xyz == 123) printf("Hello World");

You don't have this problem with the GNUC method.
0
 

Author Comment

by:LO19810527
ID: 6896020
jkr,
I tried your gcc method, but it failed to compile.
It seems like the correct syntax is the one posted by Axter.

Axter,
I tried your macro, and much to my surprise, it works on all the compilers I tested.  I did modify it a little.

#ifdef DEBUG
#ifdef __GNUC__
#define DEBUG_OUTPUT(args...) print_debug(args)
#else
#define DEBUG_OUTPUT print_debug
#endif //__GNUC__
#else  
#ifdef __GNUC__
#define DEBUG_OUTPUT(args...)
#else
#define DEBUG_OUTPUT ;/##/
#endif //__GNUC__
#endif //DEBUG

I added ";" before /##/
This will remove the bad side effect you posted.  The only down side to this modification is that it produces a warning on some compilers.  But I rather get the warning then to have the side effects.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6896026
Just out of curiousity, what compilers did you test it on?
0
 

Author Comment

by:LO19810527
ID: 6896048
I tested it in Visual C++ 6.0, Window/DOS GNU compiler, Borland (BC4), C++ Builder 5, Turbo C++ 3.0 and 4.5

With the modification I made, it gave a warning in Turbo C++ 3.0 and 4.5
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Compile GLUT with Visual Studio 2015 1 202
is twain_32.dll cmpatible with windows 10 ? 10 226
How to gracefully close the c++ 11 thread? 3 119
What is sub-make ? 2 86
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question