C++ and Anonymous Variadic Macros

J

jois.de.vivre

Hi,

I'm writing a C++ program that uses C style macros for debugging
purposes. I have code that looks something like this:


#define DEBUG(str, ...) Show(__FILE__, __LINE__, str, ## __VA_ARGS__);

void Show(const char* file, int line, const char* display, ...)
{
char display2[256];
char linestr[10];

va_list va;
va_start(va, display);
vsprintf(display2, display, va);
va_end(va);
sprintf(linestr, "%d", line);

cout << file << " [" << linestr << "]\t" << display2 << endl;
}


This is used in the following manner:

void showNum(int num)
{
DEBUG("The number is %d", num);
}

A sample output would be something like:

show.cpp [35] The number is 15


This is all fine and dandy, but everytime I use this I get the
following warning:

warning: anonymous variadic macros were introduced in C99

Moreover, if I use DEBUG without any additional arguments, for example:

void showHelloWorld()
{
DEBUG("Hello World");
}

I get the following warning in addition to the one above:

warning: ISO C99 requires rest arguments to be used

This appears in every time I use this macro and as a result appears
hundreds of times during compilation. Unfortunately, the warning isn't
very informative, and I'm not sure whether it's one I should heed. If
i'm supposed to ignore it, how can I disable the compiler from
outputting it? In searching for a solution I found that using -std=c99
instead of ansi is supposed to remove this warning, but the -std=c99
option seems to be incompatible with C++.

I'm using GCC 3.4.1 on Suse Linux 9.2

Thanks for any help.

Prashant
 
J

Jack Klein

Hi,

I'm writing a C++ program that uses C style macros for debugging
purposes. I have code that looks something like this:

Then your code is using a gcc extension and is really off-topic here.
There are no variadic macros in C++.
#define DEBUG(str, ...) Show(__FILE__, __LINE__, str, ## __VA_ARGS__);

Certainly not C++.
I'm using GCC 3.4.1 on Suse Linux 9.2

If you want help turning off a diagnostic for a non-standard gcc
extension, try the gnu.gcc.* groups or perhaps
It is certainly not a language
issue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top