fusion :: nview和const头痛

时间:2012-08-26 09:02:45

标签: c++ boost vector const boost-fusion

我被困在代码中一段时间​​了。我可以解决这个问题,但如果我甚至无法编译代码,我会觉得非常糟糕。问题在代码中描述。

namespace fusion = boost::fusion;


  template <int N, typename T>
  struct viewTraits
  {

    typedef typename T::value_type value_type;

    typedef typename fusion::result_of::as_nview<value_type, N>::type view_type;

    typedef std::vector<view_type> result_type;
  };


  int main ()
  {

    typedef boost::fusion::vector<int, double> VecType;

    typedef std::vector<VecType> Matrix;

    typedef viewTraits<0, Matrix>::result_type R;

    Matrix m (20);
    for (int i = 0; i < 20; ++i)
    {
      m[i] = VecType (i, i+0.1*i);
    }
    R r;

    /*
    * the following can compile, but not what I want
    */
    BOOST_FOREACH (Matrix::value_type v, m)
    {
      r.push_back (fusion::as_nview <0>(v) );
    }

    /*
    * the following cannot compile???, why???
    */
    BOOST_FOREACH (Matrix::value_type const &v, m)
    {
      r.push_back (fusion::as_nview <0>(v) );
    }
  }

任何人都可以指出我错过了什么。

1 个答案:

答案 0 :(得分:0)

BOOST_FOREACH期望循环变量作为第一个参数,因此它不能是const(并且因为它是一个宏而对逗号很挑剔)。看看the documented pitfalls