2014-06-20 44 views
1

我想在的for_each的Mpl的for_each與MPL名單

#include <boost/mpl/list.hpp> 
#include <algorithm> 
#include <boost/mpl/for_each.hpp> 
#include <string> 
#include <istream> 
#include <ostream> 
#include <sstream> 
#include <iostream> 
#include <boost/mpl/range_c.hpp> 
#include <boost/mpl/vector.hpp> 
#include <boost/mpl/vector_c.hpp> 
#include <boost/mpl/at.hpp> 
#include <boost/mpl/placeholders.hpp> 
#include <boost/bind.hpp> 


using namespace std; 

namespace mpl = boost::mpl; 


typedef mpl::range_c<char,1,5> range5; 
typedef mpl::list< 
mpl::int_<1> 
, mpl::int_<5> 
, mpl::int_<31> 
, mpl::int_<14> 
, mpl::int_<51> 
> inp_type; 

typedef mpl::list< 
mpl::int_<1> 
, mpl::int_<5> 
, mpl::int_<31> 
, mpl::int_<14> 
, mpl::int_<51> 
> out_type; 


template <class T> struct id {}; 
struct do_this_wrapper { 
    static char stat_c ; 
    typedef void result_type; 

    template<typename U> inline void operator()(int i, U) 
    { 
     if (i == U::value) 
     { 
      do_this_wrapper::stat_c = mpl::at_c<out_type,U::value>::type::value; 
     } 
    } 
}; 


char do_this_wrapper::stat_c ; 


int main() 
{ 

    int x; 
    std::cin>>x; 
    boost::mpl::for_each<inp_type>(boost::bind(do_this_wrapper(), x, _1)); 

    return do_this_wrapper::stat_c; 
}; 

使用mpl_list我有這些錯誤

*... /usr/include/boost/mpl/list/aux_/iterator.hpp: In instantiation of ‘struct boost::mpl::deref >’: /usr/include/boost/mpl/aux_/at_impl.hpp:37:45: required from ‘struct boost::mpl::at_impl::apply, mpl_::int_<5>, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> >, mpl_::long_<5l> >’ /usr/include/boost/mpl/at.hpp:42:8: required from ‘struct boost::mpl::at_c, mpl_::int_<5>, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> >, 5l>’ ../src/TestProj3.cpp:2664:41: required from ‘void do_this_wrapper::operator()(int, U) [with U = mpl_::int_<5>]’ /usr/include/boost/bind/bind.hpp:313:34: required from ‘void boost::_bi::list2::operator()(boost::_bi::type, F&, A&, int) [with F = do_this_wrapper; A = boost::_bi::list1&>; A1 = boost::_bi::value; A2 = boost::arg<1>]’ /usr/include/boost/bind/bind_template.hpp:32:59: required from ‘boost::_bi::bind_t::result_type boost::_bi::bind_t::operator()(A1&) [with A1 = mpl_::int_<5>; R = boost::_bi::unspecified; F = do_this_wrapper; L = boost::_bi::list2, boost::arg<1> >; boost::_bi::bind_t::result_type = void]’ /usr/include/boost/mpl/for_each.hpp:75:25: required from ‘static void boost::mpl::aux::for_each_impl::execute(Iterator*, LastIterator*, TransformFunc*, F) [with Iterator = boost::mpl::l_iter, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> > >; LastIterator = boost::mpl::l_iter; TransformFunc = boost::mpl::identity; F = boost::_bi::bind_t, boost::arg<1> > >]’ /usr/include/boost/mpl/for_each.hpp:79:111: required from ‘static void boost::mpl::aux::for_each_impl::execute(Iterator*, LastIterator*, TransformFunc*, F) [with Iterator = boost::mpl::l_iter, mpl_::int_<5>, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> > >; LastIterator = boost::mpl::l_iter; TransformFunc = boost::mpl::identity; F = boost::_bi::bind_t, boost::arg<1> > >]’ /usr/include/boost/mpl/for_each.hpp:101:97: required from ‘void boost::mpl::for_each(F, Sequence*, TransformOp*) [with Sequence = boost::mpl::list, mpl_::int_<5>, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> >; TransformOp = boost::mpl::identity; F = boost::_bi::bind_t, boost::arg<1> > >]’ /usr/include/boost/mpl/for_each.hpp:111:38: required from ‘void boost::mpl::for_each(F, Sequence*) [with Sequence = boost::mpl::list, mpl_::int_<5>, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> >; F = boost::_bi::bind_t, boost::arg<1> > >]’ ../src/TestProj3.cpp:2678:77: required from here /usr/include/boost/mpl/list/aux_/iterator.hpp:39:33: error: no type named ‘item’ in ‘struct boost::mpl::l_end’ typedef typename Node::item type;^/usr/include/boost/mpl/list/aux_/iterator.hpp: In instantiation of ‘struct boost::mpl::next >’: /usr/include/boost/mpl/aux_/preprocessed/gcc/advance_forward.hpp:68:44: required from ‘struct boost::mpl::aux::advance_forward<4l>::apply 
/usr/include/boost/mpl/aux_/preprocessed/gcc/apply_wrap.hpp:36:8: 
       required from ‘struct boost::mpl::apply_wrap1, boost::mpl::l_iter > > >’ /usr/include/boost/mpl/aux_/preprocessed/gcc/advance_forward.hpp:83:21: required from ‘struct boost::mpl::aux::advance_forward<27l>::apply ’ /usr/include/boost/mpl/aux_/preprocessed/gcc/apply_wrap.hpp:36:8: 
       required from ‘struct boost::mpl::apply_wrap1, boost::mpl::l_iter > > >’ /usr/include/boost/mpl/aux_/preprocessed/gcc/advance_forward.hpp:92:21: required from ‘struct boost::mpl::aux::advance_forward<31l>::apply, mpl_::int_<5>, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> > > >’ /usr/include/boost/mpl/aux_/preprocessed/gcc/apply_wrap.hpp:36:8: [ skipping 7 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ] /usr/include/boost/bind/bind_template.hpp:32:59: required from ‘boost::_bi::bind_t::result_type boost::_bi::bind_t::operator()(A1&) [with A1 = mpl_::int_<31>; R = boost::_bi::unspecified; F = do_this_wrapper; L = boost::_bi::list2, boost::arg<1> >; boost::_bi::bind_t::result_type = void]’ /usr/include/boost/mpl/for_each.hpp:79:111: recursively required from ‘static void boost::mpl::aux::for_each_impl::execute(Iterator*, LastIterator*, TransformFunc*, F) [with Iterator = boost::mpl::l_iter, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> > >; LastIterator = boost::mpl::l_iter; TransformFunc = boost::mpl::identity; F = boost::_bi::bind_t, boost::arg<1> > >]’ /usr/include/boost/mpl/for_each.hpp:79:111: required from ‘static void boost::mpl::aux::for_each_impl::execute(Iterator*, LastIterator*, TransformFunc*, F) [with Iterator = boost::mpl::l_iter, mpl_::int_<5>, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> > >; LastIterator = boost::mpl::l_iter; TransformFunc = boost::mpl::identity; F = boost::_bi::bind_t, boost::arg<1> > >]’ /usr/include/boost/mpl/for_each.hpp:101:97: required from ‘void boost::mpl::for_each(F, Sequence*, TransformOp*) [with Sequence = boost::mpl::list, mpl_::int_<5>, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> >; TransformOp = boost::mpl::identity; F = boost::_bi::bind_t, boost::arg<1> > >]’ /usr/include/boost/mpl/for_each.hpp:111:38: required from ‘void boost::mpl::for_each(F, Sequence*) [with Sequence = boost::mpl::list, mpl_::int_<5>, mpl_::int_<31>, mpl_::int_<14>, mpl_::int_<51> >; F = boost::_bi::bind_t, boost::arg<1> > >]’ ../src/TestProj3.cpp:2678:77: required from here /usr/include/boost/mpl/list/aux_/iterator.hpp:45:43: error: no type named ‘next’ in ‘struct boost::mpl::l_end*’ 

會心存感激的詳細descriprtion或某物閱讀。在此先感謝

回答

2

的問題是

do_this_wrapper::stat_c = mpl::at_c<out_type,U::value>::type::value;

這要求MPL找到在out_type對應的U值位置的元素。由於inp_type的值爲51(除此之外),因此它無法返回位置爲51的元素out_type,因爲它只包含5個元素。

就我個人而言,我會嘗試將兩個序列加入一個關聯序列(mpl::map),然後使用for_each。