Boost logo

Boost :

Subject: Re: [boost] [mpl] mpl::c_str fails for mpl::push_back
From: Aleksey Gurtovoy (agurtovoy_at_[hidden])
Date: 2010-11-08 05:06:42


On Sun, 07 Nov 2010 02:40:13 -0600, vicente.botet
<vicente.botet_at_[hidden]> wrote:
> I have tried to add a value_type trait
>
> template<typename Sequence>
> struct value_type {
> typedef typename Sequence::value_type type;
> };
>
> changed the c_str to use the type trait
>
> template<typename Sequence>
> struct c_str
> {
> typedef typename mpl::end<Sequence>::type iend;
> typedef typename mpl::begin<Sequence>::type i0;
> #define M0(z, n,
> data)
> \
> typedef
> \
> typename mpl::aux_::next_unless<BOOST_PP_CAT(i, n),
> iend>::type \
> BOOST_PP_CAT(i, BOOST_PP_INC(n));
> BOOST_PP_REPEAT(BOOST_MPL_LIMIT_STRING_SIZE, M0, ~)
> #undef M0
>
> typedef c_str type;
> static typename value_type<Sequence>::type const
> value[BOOST_MPL_LIMIT_STRING_SIZE+1];
> };
>
>
> and defined the following specializations:
>
> namespace boost { namespace mpl {
> template<typename T, T N>
> struct value_type<integral_c<T,N> > {
> typedef T type;
> };
> template<typename T0,typename T1,typename T2,typename T3,typename T4,
> typename T5,typename T6,typename T7,typename T8,typename T9,
> typename T10,typename T11>
> struct value_type<vector12<T0,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11> > {
> typedef typename value_type<T0>::type type;
> };
> }
> }
>
> Then we are able to use c_str on any sequence providing the
> specialization. Of course compile checking that the sequence size is not
> greater than BOOST_MPL_LIMIT_STRING_SIZE should be convenient.

A more general approach would be something like this (untested):

     BOOST_MPL_HAS_XXX_TRAIT_DEF( value_type )

     template< bool, typename X > struct value_type_impl
     {
         typedef typename X::value_type type;
     }

     template< typename X > struct value_type
         : value_type_impl< has_value_type<X>::value, X >
     {
     };

     template< typename X > struct value_type_impl<false,X>
     {
         typedef typename value_type< typename front<X>::type >::type type;
     };

> Is there a possibility to add this trait to Boost.MPL and the
> corresponding specialization and move the c_str function to a standalone
> file ?

If it's OK with Eric, I'd be happy to accept the patch.

-- 
Aleksey Gurtovoy
MetaCommunications Engineering

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk