Boost logo

Boost Users :

From: Paul Mensonides (pmenso57_at_[hidden])
Date: 2006-06-14 16:31:28


> -----Original Message-----
> From: boost-users-bounces_at_[hidden]
> [mailto:boost-users-bounces_at_[hidden]] On Behalf Of Chris Weed

> Hi,
> I am trying to use the preprocessor iteration ability to
> create typedef permutations.
>
> I want to do something like use the following set of types
> TYPE1: char int float
> TYPE2: char int float double
>
> to generate permutations of MyClass<TYPE1,TYPE2> such as
>
> typedef MyClass<char,char> Mycc;
> typedef MyClass<char,int> Myci;
> typedef MyClass<char,float> Mycf;
> ...

Creating the permutations is easy, but you can't automatically extract the first
character of an identifier (e.g. 'char' is an identifier to the preprocessor) to
generate the "unique" typedef name. The best that you can do is either pass it
along as a separate field (what the code below does) or provide a way to obtain
it through some kind of type registration.

Regards,
Paul Mensonides

#include <boost/preprocessor/seq/cat.hpp>
#include <boost/preprocessor/seq/elem.hpp>
#include <boost/preprocessor/seq/enum.hpp>
#include <boost/preprocessor/seq/for_each_product.hpp>
#include <boost/preprocessor/seq/transform.hpp>

#define A(r, product) \
    typedef MyClass< \
        BOOST_PP_SEQ_ENUM(BOOST_PP_SEQ_TRANSFORM(B, 1, product)) \
> \
    BOOST_PP_SEQ_CAT((My) BOOST_PP_SEQ_TRANSFORM(B, 0, product)); \
    /**/
#define B(s, i, elem) BOOST_PP_SEQ_ELEM(i, elem)

#define TYPE(alpha, id) ((alpha)(id))

BOOST_PP_SEQ_FOR_EACH_PRODUCT(
    A,
    ( TYPE(c, char) TYPE(i, int) TYPE(f, float) )
    ( TYPE(c, char) TYPE(i, int) TYPE(f, float) TYPE(d, double) )
)

#undef A
#undef B
#undef TYPE


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