Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I further specialize this template idea?
    primarykey
    data
    text
    <p>I can do this with a template specialization I think, for nestedness of 1,2,3 (most common cases) by respectively nesting 1,2,3 for loops and referring to the types by their typenames in stl...but for arbitrary depth, without use of the preprocessor, is there a way to do this? Maybe with mpl? Or would I need a preprocessor tool as well? Right now I am doing something like this:</p> <pre><code>template&lt;typename T, int&gt; struct MapDump {}; template&lt;typename T &gt; struct MapDump&lt;T,1&gt; { static void dump(const T&amp; map, string file, string header="") { if (!header.empty()) cout &lt;&lt; header &lt;&lt; endl; for (typename T::const_iterator cIt = map.begin(); cIt != map.end(); ++cIt) cout &lt;&lt; cIt-&gt;first &lt;&lt; "," &lt;&lt; cIt-&gt;second &lt;&lt; endl; } }; template&lt;typename T &gt; struct MapDump&lt;T,2&gt; { static void dump(const T&amp; map, string file, string header="") { if (!header.empty()) cout &lt;&lt; header &lt;&lt; endl; for (typename T::const_iterator it1 = map.begin(); it1 != map.end(); ++it1) for (typename T::mapped_type::const_iterator it2 = it1-&gt;second.begin(); it2 != it1-&gt;second.end(); ++it2) cout &lt;&lt; it1-&gt;first &lt;&lt; "," &lt;&lt; it2-&gt;first &lt;&lt; "," &lt;&lt; it2-&gt;second &lt;&lt; endl; } }; </code></pre> <p>which I can call with, for example:</p> <pre><code> map&lt;int, map&lt;int, double&gt; &gt; m; m[1][1] = 1.0; m[1][2] = 1.0; m[2][1] = 2.0; m[2][2] = 2.0; MapDump&lt; map&lt;int, map&lt;int, double&gt; &gt;, 2 &gt;::dump(m, "test.csv"); </code></pre> <p>(I stripped out the fstream stuff and left std::cout to simplify the sample code here) My question is, how can I go about specializing when, say, the last level mapped_type is a container type? For example map > is technically a 2-depth construct and not a one level construct...but my nest of 2 specialization wouldn't compile for that type...any other suggestions on how to, perhaps abstract thsi further (figure out the depth of the construct at compile time as well) are welcome..thanks!</p>
    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