By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,272 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,272 IT Pros & Developers. It's quick & easy.

variadic macro

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.