Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's the idiomatic way to traverse a boost::mpl::list?
    text
    copied!<p><strong>Edit:</strong> I've edited the sample to better resemble the problem I have, now the function depends on a regular parameter (and not only on template parameters) which means that the computations <strong>can't</strong> be made at compile time. </p> <hr> <p>I wrote some code with a hand written <a href="http://www.ddj.com/cpp/184403813" rel="nofollow noreferrer">typelist</a> and now we've started using <a href="http://www.boost.org" rel="nofollow noreferrer">boost</a> and I'm trying to move it to the <code>mpl</code> library.</p> <p>I can't seem to find any decent documentation for <code>mpl::list</code> and I'm even failing to port the code to <code>boost::mpl</code>. I've got the feeling that even when (if?) I do succeed in porting the code it will still not be idiomatic. Can' you please let me know how the following should be written with <code>boost</code> (note that this is not the actual code, it's a contrived simplification).</p> <p><strong>Original code</strong> <a href="http://codepad.org/0unH4XxA" rel="nofollow noreferrer">(codepad.org paste)</a></p> <pre><code>class nil {}; template &lt;class Head, class Tail = nil&gt; struct type_list { typedef Head head; typedef Tail tail; }; template &lt;class List&gt; struct foo; template &lt;class Head, class Tail&gt; struct foo&lt;type_list&lt;Head, Tail&gt; &gt;{ template &lt;class T&gt; static void* bar(T* obj, size_t size) { if (sizeof(Head) == size) return reinterpret_cast&lt;Head*&gt;(obj); // Otherwise check the rest of the list return foo&lt;Tail&gt;::bar(obj, size); } }; template &lt;&gt; struct foo&lt;nil&gt; { template &lt;class T&gt; static void* bar(T*, size_t) { return NULL; } }; #include &lt;iostream&gt; int main() { int n = 3; void *p = foo&lt;type_list&lt;char, type_list&lt;bool, type_list&lt;double, type_list&lt;long&gt; &gt; &gt; &gt; &gt;::bar(&amp;n, 4); std::cout&lt;&lt; p &lt;&lt; std::endl; } </code></pre> <p><strong>Failed Attempt to use Boost</strong> <a href="http://codepad.org/rXj1Aq2u" rel="nofollow noreferrer">(codepad.org paste)</a></p> <pre><code>#include &lt;boost/mpl/list.hpp&gt; template &lt;class List&gt; struct foo{ template &lt;class T&gt; static void* bar(T* obj, size_t size) { typedef typename boost::mpl::front&lt;List&gt;::type type; if (sizeof(type) == size) return reinterpret_cast&lt;type*&gt;(obj); // Otherwise check the rest of the list return foo&lt;typename List::next&gt;::bar(obj, size); } }; template &lt;&gt; struct foo&lt;boost::mpl::list0&lt;boost::mpl::na&gt; &gt; { template &lt;class T&gt; static void* bar(T*) { return NULL; } }; #include &lt;iostream&gt; int main() { int n = 3; void *p = foo&lt;boost::mpl::list&lt;char, bool, double, long&gt; &gt;::bar(&amp;n, 4); std::cout &lt;&lt; p &lt;&lt; std::endl; } </code></pre>
 

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