470,870 Members | 1,404 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,870 developers. It's quick & easy.

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 2131
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Trent Buck | last post: by
7 posts views Thread by Michael B Allen | last post: by
3 posts views Thread by Thomas Carter | last post: by
5 posts views Thread by Bill Pursell | last post: by
12 posts views Thread by Laurent Deniau | last post: by
8 posts views Thread by keith | last post: by
8 posts views Thread by Christof Warlich | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.