Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One solution that springs to mind is partially specialized templates, which is what Martin York posted, but with a twist.</p> <p>I would recommend a special content_type-struct to supply the layout type, like so:</p> <pre><code>// content for non float types template&lt;typename T&gt; struct content_type { typedef typename T member_type; member_type x,y,z; member_type&amp; X { return x; } // ... // if access to optional members is needed, better use CT_ASSERT or similar member_type&amp; Pad { char assert_error_no_pad_here[0]; } }; // content for float types struct content_type&lt;float&gt; { typedef typename float member_type; member_type x, y, z, pad; member_type&amp; X { return x; } // ... member_type&amp; Pad { return pad; } }; template&lt;typename T&gt; class Vector3 { typedef typename content_type&lt;T&gt; layout_type; typedef typename content_type&lt;T&gt;::member_type member_type; layout_type _content; public: member_type&amp; X { return _content.X(); } memmber_type&amp; Pad { return _content.Pad(); } }; // or maybe, if memory layout is not important, just inherit (watch for virtual members) template&lt;typename T&gt; class Vector3 : public content_type&lt;T&gt; { typedef typename content_type&lt;T&gt; layout_type; typedef typename content_type&lt;T&gt;::member_type member_type; }; </code></pre> <p>The advantage is you only have to write Vector3 with all of its logic once.</p> <p>You need a moderately recent compiler to do that correctly, though (MSVC>7, gcc>3)</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. 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