2012-08-26 66 views
0

我一直在代碼中停留了一段時間。我可以解決這個問題,但如果我甚至無法獲得編譯代碼,感覺非常糟糕。代碼中描述了這些問題。fusion :: nview和const頭疼

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)); 
    } 
    } 

請任何人都可以指出我錯過了什麼。

+0

什麼是編譯器錯誤信息? – 2012-08-26 09:41:20

+0

沒有匹配函數調用â€:: vector ,boost :: mpl :: vector_c ,std :: allocator ,boost :: mpl :: vector_c >>> :: push_back(boost :: fusion :: nview ,boost :: mpl :: vector_c >)stl_vector.h:602:note:candidates are:void std :: vector <_Tp, _Alloc> :: push_back(const _Tp&)[with _Tp = nview Wood

回答

0

BOOST_FOREACH期望循環變量作爲第一個參數,所以不能是const(因爲它是一個宏,所以它對於逗號是挑剔的)。看看the documented pitfalls