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 interested to allow for no
arguments at all. Is this possible? 3 2586
Thomas Carter wrote: 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 interested to allow for no arguments at all. Is this possible?
From Steele/Harbison
'When such a macro is invoked, there must be as many actual arguments as
there are identifiers in /indentier_list/'
#define name(/indentier_list/, ...) /sequence-of-tokens/
From that I would infer from that, there may indeed be zero actual
parameters passed to 'name'.
This is also how it works in gcc - which may or may not have this right
currently.
However, why would you want this? Unless you wanted to do something like?
#define mkstr(a) a "\0"
#define PRINT(...) printf("%s\n", * mkstr(__VA_ARGS __) == '\0' ? "" :
mkstr(__VA_ARGS __))
e.g.,
PRINT(); // does a \n
Expansion:
printf("%s\n", * "\0" == '\0' ? "" : "\0");
PRINT("Boo " "Hoo") // outputs Boo Hoo.
Expansion:
printf("%s\n", * "Boo " "hoo" "\0" == '\0' ? "" : "Boo " "hoo" "\0");
--
==============
*Not a pedant*
==============
On Thu, 23 Feb 2006 14:06:17 +0000, pemo wrote: Thomas Carter wrote: 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 interested to allow for no arguments at all. Is this possible? From Steele/Harbison
'When such a macro is invoked, there must be as many actual arguments as there are identifiers in /indentier_list/'
#define name(/indentier_list/, ...) /sequence-of-tokens/
From that I would infer from that, there may indeed be zero actual parameters passed to 'name'.
This is also how it works in gcc - which may or may not have this right currently.
You are right. When I tested with my original code in gcc I got an error,
because when I invoked my macro without arguments it got expanded into a
syntactically illegal C statement.
However, why would you want this? Unless you wanted to do something like?
#define mkstr(a) a "\0"
#define PRINT(...) printf("%s\n", * mkstr(__VA_ARGS __) == '\0' ? "" : mkstr(__VA_ARGS __))
e.g.,
PRINT(); // does a \n
Expansion:
printf("%s\n", * "\0" == '\0' ? "" : "\0");
PRINT("Boo " "Hoo") // outputs Boo Hoo.
Expansion:
printf("%s\n", * "Boo " "hoo" "\0" == '\0' ? "" : "Boo " "hoo" "\0");
pemo <us***********@ gmail.com> wrote: Thomas Carter wrote: 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 interested to allow for no arguments at all. Is this possible? From Steele/Harbison
'When such a macro is invoked, there must be as many actual arguments as there are identifiers in /indentier_list/'
#define name(/indentier_list/, ...) /sequence-of-tokens/
From that I would infer from that, there may indeed be zero actual parameters passed to 'name'.
All true.
This is also how it works in gcc - which may or may not have this right currently.
IIRC, gcc is different - it removes comma before __VA_ARGS__ if it is
empty, which the Standard specification doesn't. For this reason
it is advised to skip the last mandatory parameter:
int fprintf(FILE *stream, const char *format, ...);
#define FPRINTF(stream, ...) fprintf(stream, __VA_ARGS__) //dropped `format'
(I'm writing from memory, if I'm wrong I hope someone will correct me.)
However, why would you want this? Unless you wanted to do something like?
#define mkstr(a) a "\0"
#define PRINT(...) printf("%s\n", * mkstr(__VA_ARGS __) == '\0' ? "" : mkstr(__VA_ARGS __))
Why not simply:
#define PRINT(...) printf("%s\n", __VA_ARGS__ "")
or even:
#define PRINT(...) puts(__VA_ARGS_ _ "")
?
PRINT(); // does a \n
.... PRINT("Boo " "Hoo") // outputs Boo Hoo.
and
PRINT("Boo ", "Hoo") // outputs diagnostic (by your definition)
I'm not quite sure (no time to check, I once knew but forgot; again
someone please check this), I believe in C99 (in C90 it is UB) macro
arguments may be empty, so perhaps this version might be better:
#define PRINT(slit_opt) puts(slit_opt "")
--
Stan Tobias
mailx `echo si***@FamOuS.Be dBuG.pAlS.INVALID | sed s/[[:upper:]]//g` This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: jois.de.vivre |
last post by:
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, ...)
{
|
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, variadic arithmetic and boolean operators.
For example,
product (2, sum (3, 4), 5);
|
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 char* formatter, ...);
Then, I want to call it as so:
|
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:
|
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 argument, value is %d\n", i);
DBG("two arguments: int %d, strings %s\n", i, s);
| |
by: jontwang |
last post by:
How do you get a count of the number of arguments passed into a
variadic macro?
|
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
B() -nothing, *warning ISO C99 requires rest arguments to be used*
|
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 function are of type int.
(My question has nothing to do with the definition of the function
foo, so don't bother about it.)
If I call the function as:
foo(2,3,4,5,6,7,8);/*More arguments than expected*/
|
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
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |