Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing conditional statement to select different templated matrix class at run-time
    primarykey
    data
    text
    <p>I'm working with the templated Matrix class in the Eigen linear algebra library (<a href="http://eigen.tuxfamily.org/dox/TutorialMatrixClass.html" rel="nofollow">link</a>). The Matrix class takes three nominal template parameters:</p> <pre><code>Matrix&lt;type, rows, cols&gt; </code></pre> <p>In the above, an example of <code>type</code> is <code>double</code>, or <code>std::complex&lt;double&gt;</code>. In addition, <code>rows</code> is the number of rows, and <code>cols</code> is the number of columns in the Matrix.</p> <p>As shown by the code below, what I would like to do is use a different templated Matrix class at run-time using a conditional statement.</p> <p>The first solution that comes to mind might be to use void pointers.</p> <pre><code>#include &lt;iostream&gt; #include &lt;Eigen/Dense&gt; #include &lt;complex&gt; using namespace Eigen; int main() { // this flag is set at run-time, but it is only set here // in the code as an example int create_complex = 1; void *M; if(create_complex) { Matrix&lt;std::complex&lt;double&gt;,3,3&gt; m0; M = &amp;m0; } else { Matrix&lt;double,3,3&gt; m0; M = &amp;m0; } // de-reference pointer here and use it return 0; } </code></pre> <p>Although this code compiles, the <code>void *M</code> pointer needs to be explicitly de-referenced before use. This is inconvenient, since I then have to write different code blocks for the same program logic.</p> <p>I am wondering if there is something similar to polymorphism that might be applied here, where I don't have to use void pointers.</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.
    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