c++stringboostboost-mpl

concatenate boost::mpl::string


How I can concatenate boost::mpl::string? The following code produce errors:

#include <iostream>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/string.hpp>
#include <boost/mpl/fold.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/mpl/push_back.hpp>

typedef boost::mpl::vector<
   boost::mpl::string<'ab'>,
   boost::mpl::string<'cd'>,
   boost::mpl::string<'ef'>
> slist;

typedef boost::mpl::fold<
   slist,
   boost::mpl::string<>,
   boost::mpl::push_back<boost::mpl::_1, boost::mpl::_2>
>::type string;

int main() {
   std::cout << boost::mpl::c_str<string>::value << std::endl;
}

errors:

In file included from source.cpp:3:0: string.hpp: In instantiation of 'boost::mpl::push_back_impl< boost::mpl::string_tag >::applyboost::mpl::string<, boost::mpl::string<24930> >': boost/mpl/push_back.hpp:32:1:
instantiated from 'boost::mpl::push_back< boost::mpl::string<>, boost::mpl::string<24930> >' boost/mpl/aux_/has_type.hpp:20:1:
instantiated from 'const bool boost::mpl::aux::has_type< boost::mpl::push_backboost::mpl::string<, boost::mpl::string<24930> >, mpl_::bool_ >::value' boost/mpl/aux_/has_type.hpp:20:1:
instantiated from 'boost::mpl::aux::has_type< boost::mpl::push_backboost::mpl::string<, boost::mpl::string<24930> >, mpl_::bool_ >' boost/mpl/aux_/preprocessed/gcc/quote.hpp:56:5: instantiated from 'boost::mpl::quote2< boost::mpl::push_back >::applyboost::mpl::string<, boost::mpl::string<24930> >' boost/mpl/aux_/preprocessed/gcc/apply_wrap.hpp:49:1: instantiated from 'boost::mpl::apply_wrap2< boost::mpl::quote2boost::mpl::push_back, boost::mpl::string<>, boost::mpl::string<24930> >' boost/mpl/aux_/preprocessed/gcc/bind.hpp:207:21: instantiated from 'boost::mpl::bind2< boost::mpl::quote2boost::mpl::push_back, mpl_::arg<1>, mpl_::arg<2> >::applyboost::mpl::string<, boost::mpl::string<24930> >' boost/mpl/aux_/preprocessed/gcc/apply_wrap.hpp:49:1: instantiated from 'boost::mpl::apply_wrap2< boost::mpl::protect< boost::mpl::bind2boost::mpl::quote2<boost::mpl::push_back, mpl_::arg<1>, mpl_::arg<2> >, 0 >, boost::mpl::string<>, boost::mpl::string<24930> >' boost/mpl/aux_/preprocessed/gcc/apply.hpp:73:1: instantiated from 'boost::mpl::apply2< boost::mpl::push_back<mpl_::arg<1>, mpl_::arg<2> >, boost::mpl::string<>, boost::mpl::string<24930> >' boost/mpl/aux_/preprocessed/gcc/fold_impl.hpp:87:85: instantiated from 'boost::mpl::aux::fold_impl< 3, boost::mpl::v_iter< boost::mpl::vectorboost::mpl::string<24930, boost::mpl::string<25444>, boost::mpl::string<25958> >, 0l >, boost::mpl::v_iter< boost::mpl::vectorboost::mpl::string<24930, boost::mpl::string<25444>, boost::mpl::string<25958> >, 3l >, boost::mpl::string<>, boost::mpl::push_back<mpl_::arg<1>, mpl_::arg<2> > >' boost/mpl/fold.hpp:39:18: instantiated from 'boost::mpl::fold< boost::mpl::vectorboost::mpl::string<24930, boost::mpl::string<25444>, boost::mpl::string<25958> > , boost::mpl::string<>, boost::mpl::push_back<mpl_::arg<1>, mpl_::arg<2> > >' source.cpp:18:2: instantiated from here string.hpp:207:53: error: 'value' is not a member of 'boost::mpl::string<24930>' In file included from boost/mpl/back_inserter.hpp:18:0, from boost/mpl/aux_/inserter_algorithm.hpp:18, from boost/mpl/copy.hpp:20, from string.hpp:26, from source.cpp:3: boost/mpl/push_back.hpp: In instantiation of 'boost::mpl::push_back< boost::mpl::push_backboost::mpl::string<, boost::mpl::string<24930> >, boost::mpl::string<25444> >': boost/mpl/aux_/has_type.hpp:20:1:
instantiated from 'const bool boost::mpl::aux::has_type< boost::mpl::push_back< boost::mpl::push_backboost::mpl::string<, boost::mpl::string<24930> >, boost::mpl::string<25444> >, mpl_::bool_ >::value' boost/mpl/aux_/has_type.hpp:20:1:
instantiated from 'boost::mpl::aux::has_type< boost::mpl::push_back< boost::mpl::push_backboost::mpl::string<, boost::mpl::string<24930> >, boost::mpl::string<25444> >, mpl_::bool_ >' boost/mpl/aux_/preprocessed/gcc/quote.hpp:56:5: instantiated from 'boost::mpl::quote2< boost::mpl::push_back


Solution

  • #include <iostream>
    #include <boost/mpl/vector.hpp>
    #include <boost/mpl/string.hpp>
    #include <boost/mpl/fold.hpp>
    #include <boost/mpl/front.hpp>
    #include <boost/mpl/deref.hpp>
    #include <boost/mpl/placeholders.hpp>
    #include <boost/mpl/push_back.hpp>
    
    using namespace boost;
    
    typedef boost::mpl::vector<
       boost::mpl::string<'a','b'>,
       boost::mpl::string<'c','d'>,
       boost::mpl::string<'e','f'>
    > slist;
    
    
    typedef boost::mpl::reverse_fold<
       slist,
       mpl::string<>,
       boost::mpl::copy<mpl::_1, mpl::back_inserter<mpl::_2> >
    >::type string;
    
    int main() {
       std::cout << boost::mpl::c_str<string>::value << std::endl;
    }