473,395 Members | 1,471 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,395 software developers and data experts.

variadic macro

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.

The reason why I need this: I want a fixed Index->Value mapping that should be part
of a compile time API. The straight forward way to do this would be an array, e.g.:

const int Mapper[] = {27, 225, 815, 4711, ..};

Obviously, any values could be obtained from their index through Mapper[index].

But unfortunately, this simple approach is not possible as the values need to
be const (which they aren't despite of the const keyword), because they are supposed
to be used as template parameters.

A simple but ugly way arround this is to employ templates and template specialization:

template<unsigned int indexstruct Mapper {};
template<struct Mapper<0{static const unsigned int value = 27;};
template<struct Mapper<1{static const unsigned int value = 225;};
template<struct Mapper<2{static const unsigned int value = 815;};
template<struct Mapper<3{static const unsigned int value = 4711;};
...

Similarly, any value could be obtained from its index through Mapper<index>::value.
So far, things are working fine for my needs.

But as the definition of the mapping is part of the API, I tried to wrap this into
a marco. As my index will _always_ start with 0, I'm looking for something that looks
as much as possible like the array definition, e.g. with a variadic macro:

#define MAPPER(...) ???????????????????
MAPPER(27, 225, 815, 4711, ..)

Any ideas are very welcome.

Thanks and regards,

Christof
Jun 27 '08 #1
2 2304
Christof Warlich wrote:
Hi macro experts,
C++ doesn't have variadic macros (yet?), they are a feature of C99. Try
the question (without C++ references) on comp.lang.c.

--
Ian Collins.
Jun 27 '08 #2
Happily Someone(TM) has already done that; see <url:
http://www.boost.org/doc/libs/1_35_0/libs/preprocessor/doc/index.html>.
Hi Alf,

thanks a lot for this link, it offer's a wealth of smart macro artwork :-)

Christof
Jun 27 '08 #3

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...
5
by: Bill Pursell | last post by:
I don't particularly enjoy using the va_start macro family, and I've noticed that the following code works. I'm a little concerned about the fact that the prototypes for foo do not match. Is this...
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 ...
8
by: keith | last post by:
Someone please help me out here - I'm having a 'bad brain' day. I have a number of C logging functions, prototyped as, say: extern "C" { mylog1(char *arg1, char *arg2, char *arg3);...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.