Boost logo

Boost Users :

Subject: Re: [Boost-users] large enum and native array indexing
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-01-14 13:38:49


AMDG

Hicham Mouline wrote:
> we have a 3rd party lib which provides an enum OP with some hundred members,
> but each member is defined with numbers that are not contiguous.
>
> enum OP { op1=56, op2=6, op3=5 ..... };
>
> We have a performance critical function f that looks like this:
>
> <snip>
>
>
> Now, the question is about defining the metafunction Position.
>
>
> template< OP op > struct Position {};
> template<> struct Position<OP5> { const size_t value=4; };
> ....
>
>
>
> The 3rd party enum OP in the header file I can't change.
>
> Is there a way to generate my metafunction automatically?
> Any facility in mpl or fusion instead of defining my own metafunction?
>

I would say that the easiest way is to use the preprocessor.

#define BOOST_PP_LOCAL_MACRO(n) template<> struct
Position<BOOST_PP_CAT(OP, n)> { static const size_t value = n - 1; };
#define BOOST_PP_LOCAL_LIMITS (1, MAX_OP)
#include BOOST_PP_LOCAL_ITERATE()

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