variadic macro

C

Christof Warlich

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 index> struct 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
 
I

Ian Collins

Christof said:
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.
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top