Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Hmm, first, you seem to be rolling a lot of your own stuff. I'd look into <a href="http://www.boost.org/doc/libs/1_39_0/libs/mpl/doc/index.html" rel="nofollow noreferrer">Boost::MPL</a> to replace, say Base selection template, and to inherit over a vector with inherit.</p> <p>Second, you are using "const static bool hasSpeed = true;" a lot; In my programing, I usually prefer a typedefed trait, similar to <a href="http://www.boost.org/doc/libs/1_39_0/libs/type_traits/doc/html/index.html" rel="nofollow noreferrer">Boost::type_traits</a>. You can use them to select functions to run at compile time. You could avoid doing that "EmptyBase".</p> <pre><code>template &lt;typename T&gt; struct has_size; template &lt;bool speed, bool accel, typename T&gt; struct has_size&lt; Particle&lt;true, speed, accel, T&gt; &gt; : public true_type { }; template &lt;bool speed, bool accel, typename T&gt; struct has_size&lt; Particle&lt;false, speed, accel, T&gt; &gt; : public false_type { }; // given a particle typedef SomeParticle_t has_size&lt;SomeParticle_T&gt;::value </code></pre> <p>Third, a lot of what you are doing here depends on what you want your end usage to be; do you want the particles to be separate, types that can't be converted to each other? If you go this route, pretty much every function will be a template, and you could use boost::enable_if to disable code that doesn't apply to a given particle. It will be potentially very fast (since a lot of work happens at compile time), but you'll have gigantic, hard to read error statements (not very accessible for someone new to templates).</p> <p>Another, non template route would be inheritance; you would define a set of virtual functions that a particle needs, and then break those out into classes you inherit from. It's not quite clear from your code what you expect to be able to do with the Particle. The errors would be easier to understand, but the code potentially slower (more work shifted to runtime, and would make your objects bigger)</p> <p>Here's a code sample to illustrate the differences:</p> <pre><code>// Using templates template &lt;typename particle&gt; typename boost::enable_if&lt; has_accel&lt;particle&gt;, typename particle::speed_type &gt;::type calculate_future_speed(const particle&amp; t) { /* do your acceleration calculation */ } template &lt;typename particle&gt; typename boost::enable_if&lt; boost::and_&lt; has_speed&lt;particle&gt;, boost::not_&lt; has_accel&lt;particle&gt; &gt;, typename particle::speed_type &gt;::type calculate_future_speed(const particle&amp; t) { /* no acceleration, so no calculation! just return the speed*/ } template &lt;typename particle&gt; typename boost::enable_if&lt; boost::not_&lt; has_speed&lt;particle&gt;, typename particle::speed_type &gt;::type calculate_future_speed(const particle&amp; t) { return 0; /* Has no speed, and no acceleration */ } </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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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