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
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