473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

C++ and Anonymous Variadic Macros

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

Jul 23 '05 #1
1 6609
On 4 Jul 2005 11:02:29 -0700, jo***********@gmail.com wrote in
comp.lang.c++:
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
news:comp.os.linux.development.apps. It is certainly not a language
issue.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Trent Buck | last post by:
(Note: C99 supports variadic macros, but C89 does not.) I'm pretty sure what I'm trying to do is impossible, but I'll ask here in case I'm missing something. I'm trying to define generic,...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
7
by: Michael B Allen | last post by:
If I define a variadic macro like say: #define PRINT(fmt, ...) _myprintf(__FILE__ ": " fmt, __VA_ARGS__) and I call this like: PRINT("no args"); the preprocessor generates:
3
by: Thomas Carter | last post by:
I understand that C99 supports variadic macros. However, is it not the case that a variadic macro defined as #define SAMPLE_MACRO(...) Bloody-blah must take at least one argument? I would be...
15
by: Urs Thuermann | last post by:
I want to write a macro that produces debug output and has a variable number of arguments, so that I can use like this: int i; char *s; DBG("simple message\n"); DBG("message with an int...
14
by: jontwang | last post by:
How do you get a count of the number of arguments passed into a variadic macro?
12
by: Laurent Deniau | last post by:
I was playing a bit with the preprocessor of gcc (4.1.1). The following macros expand to: #define A(...) __VA_ARGS__ #define B(x,...) __VA_ARGS__ A() -nothing, *no warning* A(x) -x ...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
8
by: Christof Warlich | last post by:
Hi, is there any way to access individual elements in the body of a variadic macro? I only found __VA_ARGS__, which always expands to the complete list. Thanks for any help, Christof
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.