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:
_myprintf("file.c" ": " "no args",);
which isn't going to work because of the comma. Is there a workaround?
This seems like a drab oversight in variadic macros.
Mike 7 1605
Michael B Allen wrote: 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:
_myprintf("file.c" ": " "no args",);
which isn't going to work because of the comma. Is there a workaround? This seems like a drab oversight in variadic macros.
Have you read the examples from the standard (or N869)?
A quick replacement is...
#define PRINT(...) myprintf(__FILE__ ": " __VA_ARGS__)
A more robust macro might be...
#define PRINT(...) \
do { myprintf("%s: ", __FILE__); \
myprintf( __VA_ARGS__ ); } while (0)
--
Peter
Michael B Allen wrote: 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:
_myprintf("file.c" ": " "no args",);
which isn't going to work because of the comma. Is there a workaround? This seems like a drab oversight in variadic macros.
Mike
Make this
#define PRINT(...) _myprintf(__FILE__ ": " __VA_ARGS__)
If you want to provide an automatic linefeed (i.e. append '\n'
to fmt), then you need a separate call:
#define PRINT(...) do { \
_myprintf(__FILE__ ": " __VA_ARGS__); \
_myputchar('\n'); \
} while (0)
(untested)
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Michael Mair wrote: Michael B Allen wrote:
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:
_myprintf("file.c" ": " "no args",);
On a second thought:
You are aware that you are invading the implementation's namespace
by the use of leading underscores? If you want to have an underscore
sticking out at an end then rather use "myprintf_"
Cheers
Michael which isn't going to work because of the comma. Is there a workaround? This seems like a drab oversight in variadic macros.
Mike
Make this #define PRINT(...) _myprintf(__FILE__ ": " __VA_ARGS__)
If you want to provide an automatic linefeed (i.e. append '\n' to fmt), then you need a separate call: #define PRINT(...) do { \ _myprintf(__FILE__ ": " __VA_ARGS__); \ _myputchar('\n'); \ } while (0) (untested)
Cheers Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
On Sat, 12 Mar 2005 05:55:05 -0500, Michael Mair wrote: _myprintf("file.c" ": " "no args",);
On a second thought: You are aware that you are invading the implementation's namespace by the use of leading underscores?
No, I wasn't. I thought <underscore><upper> was reserved, not
<underscore><lower>. Are you certain?
Mike
Michael B Allen <mb*****@ioplex.com> writes: On Sat, 12 Mar 2005 05:55:05 -0500, Michael Mair wrote:
_myprintf("file.c" ": " "no args",);
On a second thought: You are aware that you are invading the implementation's namespace by the use of leading underscores?
No, I wasn't. I thought <underscore><upper> was reserved, not <underscore><lower>. Are you certain?
C99 7.1.3p1:
All identifiers that begin with an underscore and either an
uppercase letter or another underscore are always reserved for any
use.
All identifiers that begin with an underscore are always reserved
for use as identifiers with file scope in both the ordinary and
tag name spaces.
(I tend not to remember these details; I just avoid identifers with
leading underscores.)
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Michael B Allen wrote: On Sat, 12 Mar 2005 05:55:05 -0500, Michael Mair wrote:
_myprintf("file.c" ": " "no args",);
On a second thought: You are aware that you are invading the implementation's namespace by the use of leading underscores?
No, I wasn't. I thought <underscore><upper> was reserved, not <underscore><lower>. Are you certain?
You are right but as soon as _myprintf() resides in a different
translation unit the difference gets blurry as we only have six
significant characters _without_ looking at upper or lower
case for identifiers in C89; C99 nonetheless restricts the
scope of such identifiers to file IIRC.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
On Sat, 12 Mar 2005 04:56:05 -0500, Peter Nilsson wrote: A quick replacement is...
#define PRINT(...) myprintf(__FILE__ ": " __VA_ARGS__)
Yeah, but with this you cannot reorder the arguments.
#define PRINT(err, fmt, ...) \
_myprintf(__FILE__ ": %s" fmt, errstr(err), __VA_ARGS__)
used like:
PRINT(EFOO, ": ouch!");
gives:
_myprintf("file.c" ": %s" ": ouch!", errstr(err),)
A more robust macro might be...
#define PRINT(...) \ do { myprintf("%s: ", __FILE__); \ myprintf( __VA_ARGS__ ); } while (0)
I don't think this helps in this case either.
Mike This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: TheDD |
last post by:
Hello all,
right now, i'm using the following macro to automatically add informations
to exceptions:
#define THROW(Class, args...) throw Class(__FILE__, __LINE__, ## args)
but AFAIK, it's...
|
by: Michael B Allen |
last post by:
Is the following legit?
#define PRINT(err, fmt, ...) \
my_printf(__FILE__ ": %s" fmt, errstr(err), ##__VA_ARGS__)
Without the ## if I call this like PRINT(EFOO, ": foo!") this reduces
to...
|
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
...
|
by: Christof Warlich |
last post by:
Hi macro experts,
in a variadic macro, i.e. in a macro with a variable parameter list, is there
any way to access single parameters of the list? __VA_ARGS__ only expands to
the whole list.
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |