Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In C++0x you have <code>template typedef</code> finally available!</p> <p><em>Disclaimer: nothing has been compiled...</em></p> <p>From Wikipedia's article:</p> <pre><code>template&lt; typename second&gt; using TypedefName = SomeType&lt;OtherType, second, 5&gt;; </code></pre> <p>which in your case would yield</p> <pre><code>template &lt;class Type&gt; using vector3 = vector&lt;Type, 3&gt;; </code></pre> <p>I can't tell you how much I craved for this ;)</p> <p>However it doesn't solve the parameters issue. As mentioned, you could try and use variadic templates here, however I am unsure as to their application in this case. The normal use is with recursive methods and you would need to throw a <code>static_assert</code> in the midst.</p> <p><em>Edited</em> to take the comments into account.</p> <pre><code>template &lt;class Type, size_t Size&gt; class vector { public: template &lt;class... Args&gt; vector(Args... args): data({args...}) { // Necessary only if you wish to ensure that the exact number of args // is passed, otherwise there could be less than requested BOOST_MPL_ASSERT_RELATION(sizeof...(Args), ==, Size); } private: T data[Size]; }; </code></pre> <p>Another possibility that is already available is to combine Preprocessor generation with <code>boost::enable_if</code>.</p> <pre><code>template &lt;class Type, size_t Size&gt; class vector { public: vector(Type a0, typename boost::enable_if_c&lt; Size == 1 &gt;::type* = 0); vector(Type a0, Type a1, typename boost::enable_if_c&lt; Size == 2 &gt;::type* = 0); // ... }; </code></pre> <p>Using Boost.Preprocessor for the generation makes this easier.</p> <pre><code>BOOST_PP_REPEAT(MAX_COUNT, CONSTRUCTOR_MACRO, ~); // where MAX_COUNT is defined to the maximum size you wish // and CONSTRUCTOR_MACRO actually generates the constructor #define CONSTRUCTOR_MACRO(z, n, data) \ vector( \ BOOST_PP_ENUM_PARAMS(n, Type a), \ typename boost::enable_if_c&lt; Size == n &gt;::type* = 0 \ ); </code></pre> <p>The implementation of the constructor is left as an exercise for the reader. It's another call to <code>BOOST_PP_REPEAT</code>.</p> <p>As you can see, it soon gets ugly, so you'll be better off if you can use the variadic template version.</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