Note that there are some explanatory texts on larger screens.

plurals
  1. POCan this be accomplished with templates in C++?
    text
    copied!<p>This is a very simple class that represents three rows of doubles, with some string information attached to each row:</p> <pre><code>struct ThreeRows { std::vector&lt;double&gt; row1; std::vector&lt;double&gt; row2; std::vector&lt;double&gt; row3; std::string row1info; std::string row2info; std::string row3info; }; </code></pre> <p>What I want to do is generalize this type in the following ways:</p> <ol> <li><p>The rows should not be fixed at three anymore, but any number of rows should be supported as a part of the type.</p></li> <li><p>I should be able to specify what types should be in each row. Maybe I want <code>double</code> for the first row and <code>int</code> for the second row. (Making this example a two row class.)</p></li> <li><p>Finally, I should be able to attach other information than just <code>string</code>s to the rows. For instance (continuing the example in point 2), I may want to attach a <code>string</code> to the first row but a custom <code>rss_feed</code> to the second row.</p></li> </ol> <p>If templates allowed it (which they don't), I would want to type out something like this to obtain my type:</p> <pre><code>Rows&lt;{double,int}, {string,rss_feed}&gt; </code></pre> <p>the number of rows being determined from that, or if I really had to:</p> <pre><code>Rows&lt;{double,int}, {string,rss_feed}, 2&gt; </code></pre> <p>But this is not possible.</p> <p>Can something be done, and if so, how would I work with this class template? How would I actually get at my vectors and info objects and work with them?</p>
 

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