Note that there are some explanatory texts on larger screens.

plurals
  1. POboost fusion and const correctness
    primarykey
    data
    text
    <p>I am learning boost fusion and am trying to take a view of a <code>std::vector&lt;boost::fusion::vector&lt;int,double,double&gt; &gt;</code>. The code appears simple but I appear to be running into some problems with const. I clearly misunderstand something about const and would love for someone to explain where I am going wrong.</p> <p>The code:</p> <pre><code>template&lt;int N, class T&gt; struct viewTraits{ typedef typename T::value_type etype; typedef typename boost::fusion::result_of::as_nview&lt;etype, N&gt;::type netype; typedef std::vector&lt;netype&gt; result_type; }; template &lt;int N, typename T&gt; typename viewTraits&lt;N,T&gt;::result_type c( T const &amp;t ) { typename viewTraits&lt;N, T&gt;::result_type retVal; for (typename T::const_iterator it(t.begin());it&lt;t.end();++it){ retVal.push_back(fusion::as_nview&lt;N&gt;(*it)); } return retVal; } template &lt;typename Container&gt; typename Container::value_type sum( Container const &amp;container ) { typedef typename Container::value_type value_type; return std::accumulate( container.begin(), container.end(), value_type() ); } int main(){ typedef fusion::vector&lt;int, double, double&gt; row; typedef std::vector&lt;row&gt; container; container x; b( x, 200 ); std::cout &lt;&lt; sum(c&lt;1&gt;(x)) &lt;&lt; std::endl; } </code></pre> <p>The code fails to compile at <code>retVal.push_back()</code> because of an issue with casting const. I tried several permutations of removing and adding some const keywords but have not been successful at programming by permutation and would rather understand what I am doing.</p> <p>Anyone have any thoughts? BTW the <code>const</code> in the function definition of <code>sum</code> and <code>c</code> must stay.</p> <p>EDIT: I forgot to mention that <code>b</code> fills <code>x</code> which is a <code>std::vector&lt;fusion::vector&lt;int, double, double&gt; &gt;</code></p> <p>EDIT2: The corrected code:</p> <pre><code>template&lt;int N, class T&gt; struct viewTraits{ typedef typename T::value_type etype; typedef typename fusion::result_of::as_nview&lt;etype, N&gt;::type netype; typedef typename fusion::result_of::at_c&lt;netype,0&gt;::type reference; typedef typename boost::decay&lt;reference&gt;::type value_type; typedef std::vector&lt;value_type&gt; result_type; }; template &lt;int N, typename T&gt; typename viewTraits&lt;N,T&gt;::result_type c( T const &amp;t ) { typename viewTraits&lt;N,T&gt;::result_type retVal; for(typename T::const_iterator it(t.begin()); it&lt;t.end();++it){ retVal.push_back(fusion::deref(fusion::begin(fusion::as_nview&lt;N&gt;(*it)))); } return retVal; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload