Boost logo

Boost Users :

Subject: Re: [Boost-users] large enum and native array indexing
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-01-15 12:56:56


AMDG

Hicham Mouline wrote:
> I think I wasn't clear enough:
>
> the 3rd party lib (which I can't change of course) provides,
> enum Op {
> OPone = 7,
> OPtwo = 9,
> OPthree = 45,
> OPfour = 7667 // there is no pattern in the names nor the numbers.
> }; // in fact, there are a hundred entries
>
>
> and I wished to generate from Op
>
> template<> struct Position<OPone> { static const int value = 0; }
> template<> struct Position< OPtwo > { static const int value = 1; }
> template<> struct Position< OPthree > { static const int value = 2; }
> template<> struct Position< OPfour > { static const int value = 2; }
>
> I suppose there is no way to automagically generate the template specs
> from the enum?
>

No there isn't. There is no way to introspect the members of an enum.
The best you can do would be
typedef boost::mpl::vector_c<OPone, OPtwo, ...> ops;

However, this doesn't scale well, to a hundred entries.

You might be able to use a preprocessor sequence, though

#define OPS (OPone)(OPtwo)(OPthree)...

#define OP_SPECIALIZATION(n, data, elem) template<> struct
Position<elem> { static const int value = n };
BOOST_PP_SEQ_FOR_EACH(OP_SPECIALIZATION, ~, OPS)

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net