【问题标题】:How do I iterate over a tuple我如何迭代一个元组
【发布时间】:2010-12-28 07:40:10
【问题描述】:

如何从索引 1 到 2 开始迭代元组?以下不起作用。

using boost::fusion::cons;
typedef cons<A, cons<B, cons<C, cons<D> > > > MyTuple;
MyTuple tuple_;

template <class T>
struct DoSomething{

  DoSomething(T& t) : t_(&t){ }

  template <class U>
  void operator()(U u){
    boost::fusion::at<mpl::int_<u> >(*t_);
  }
  T* t_;
};

boost::mpl::for_each< boost::mpl::range_c<int, 1, 3> >( DoSomething<MyTuple>(tuple_) );

【问题讨论】:

    标签: c++ boost


    【解决方案1】:

    我不确定您的意图,但以下代码能满足您的目的吗? 我用了fusion,而不是mpl

    struct DoSomething {
      template< class U >
      void operator()( U u ) const {
        std::cout << u << '\n'; // an example
      }
    };
    
    using namespace boost::fusion; // Sorry, for brevity
    
    iterator_range<
      result_of::advance_c< result_of::begin< MyTuple >::type, 1 >::type
    , result_of::advance_c< result_of::begin< MyTuple >::type, 3 >::type
    > ir( advance_c< 1 >( begin( tuple_ ) )
        , advance_c< 3 >( begin( tuple_ ) ) );
    for_each( ir, DoSomething() );    
    

    希望对你有帮助

    【讨论】:

    • 非常感谢,确实有效。虽然,对于我的意图,'operator()(U u)' 需要是 'operator()(U& u)'。
    • 我的意图:我有一个元组。对于每种类型,我都需要调用特定的成员函数,但并非所有类都具有该成员函数。没有反思,这是我能想到的最接近的事情。
    【解决方案2】:

    从您的评论来看, 你提到的可能通过做一个谓词来实现 确定指定类是否具有成员函数的类, 并使用fusion::filter_view。 以下代码中的DEF_HAS_MEM_FUNC宏在以下代码中解释:

    Is it possible to write a template to check for a function's existence?

    #include <boost/fusion/include/cons.hpp>
    #include <boost/fusion/include/filter_view.hpp>
    #include <boost/fusion/include/for_each.hpp>
    #include <boost/mpl/placeholders.hpp>
    using namespace boost;
    
    #define DEF_HAS_MEM_FUNC( name, func_name, signature )  \
      template< class T >                                   \
      struct name {                                         \
        template< class U, U > struct mfp;                  \
                                                            \
        template< class U >                                 \
        static char f( mfp< signature, &U::func_name >* );  \
                                                            \
        template< class > static char (&f(...))[2];         \
                                                            \
        enum { value = (sizeof( f<T>(0) ) == 1) };          \
      }
    
    DEF_HAS_MEM_FUNC( has_f, f, void(U::*)()const );
    
    struct DoSomething {
      template< class U >
      void operator()( U& u ) const {
        u.f();
      }
    };
    
    struct A {};
    
    struct B {
      void f() const {}
    };
    
    typedef fusion::cons< A, fusion::cons< B > >  MyTuple;
    
    int main()
    {
      MyTuple tuple_;
      fusion::filter_view< MyTuple const, has_f< mpl::_ > > v( tuple_ );
      fusion::for_each( v, DoSomething() );
    }
    

    【讨论】:

      猜你喜欢
      • 2011-07-14
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2017-12-10
      • 1970-01-01
      相关资源
      最近更新 更多