Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can have that for fixed-size arrays:</p> <pre><code>int a[] = { foo&lt;0&gt;::value, foo&lt;1&gt;::value, ... }; </code></pre> <p>Arbitrarily sized arrays however can't be done without preprocessor meta-programming - <a href="http://www.boost.org/doc/libs/1_42_0/libs/preprocessor/doc/index.html" rel="nofollow noreferrer">Boost.Preprocessor</a> can help here.</p> <p>Another thing you might want to look into are compile-time sequences of integral constants, e.g. using <a href="http://www.boost.org/doc/libs/1_42_0/libs/mpl/doc/index.html" rel="nofollow noreferrer">Boost.MPL</a>:</p> <pre><code>template&lt;int n&gt; struct squares { typedef typename squares&lt;n-1&gt;::type seq; typedef typename boost::mpl::integral_c&lt;int, n*n&gt;::type val; typedef typename boost::mpl::push_back&lt;seq, val&gt;::type type; }; template&lt;&gt; struct squares&lt;1&gt; { typedef boost::mpl::vector_c&lt;int, 1&gt;::type type; }; // ... typedef squares&lt;3&gt;::type sqr; std::cout &lt;&lt; boost::mpl::at_c&lt;sqr, 0&gt;::type::value &lt;&lt; std::endl; // 1 std::cout &lt;&lt; boost::mpl::at_c&lt;sqr, 1&gt;::type::value &lt;&lt; std::endl; // 4 std::cout &lt;&lt; boost::mpl::at_c&lt;sqr, 2&gt;::type::value &lt;&lt; std::endl; // 9 </code></pre> <p><em>(Note that this could probably be done more elegantly using MPLs algorithms)</em></p> <p>If you're more curious about the compile-time subject the books <em>"Modern C++ Design"</em> (TMP basics) and <em>"C++ Template Metaprogramming"</em> (mainly MPL explained in depth) are worth looking into.</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.
    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.
    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