Boost logo

Boost Users :

Subject: Re: [Boost-users] [Proto] Building a mpl::vector from an expression
From: Eric Niebler (eric_at_[hidden])
Date: 2009-08-19 04:18:55


Steven Watanabe wrote:
> christophe henry wrote:
>> I am trying to build a mpl::vector from an expression like
>>
>> (string(),ClassA(),ClassB())
>>
>> To get a mpl::vector<string,ClassA,ClassB>
>> <snip>
>>
>> Is there a way to do this or I should give up the idea?
>
> Does fold_tree help?

It does.

   #include <string>
   #include <boost/mpl/vector.hpp>
   #include <boost/mpl/push_back.hpp>
   #include <boost/proto/core.hpp>
   #include <boost/proto/transform.hpp>
   namespace proto = boost::proto;
   namespace mpl = boost::mpl;
   using proto::_;

   namespace My
   {
     struct ClassA {};
     struct ClassB {};

     template<class T> struct is_my : mpl::false_ {};
     template<> struct is_my<std::string> : mpl::true_ {};
     template<> struct is_my<ClassA> : mpl::true_ {};
     template<> struct is_my<ClassB> : mpl::true_ {};

     BOOST_PROTO_DEFINE_OPERATORS(is_my, proto::default_domain)

     struct FoldTreeToVector
       : proto::fold_tree<
             _
           , mpl::vector0<>()
           , mpl::push_back<proto::_state, proto::_value>()
>
     {};
   }

   int main()
   {
     mpl::vector3<std::string, My::ClassA, My::ClassB> x =
         My::FoldTreeToVector()((std::string(), My::ClassA(),
My::ClassB()));
   }

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.com

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