Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To expand on sbi's implementation, here's one that does exponentiation-by-squaring (which requires fewer template instantiations for large powers).</p> <p>Note that if you really only want to calculate powers of 2, you're much better off just left-shifting (i.e., <code>2**x == 1 &lt;&lt; x</code>) rather than doing all this template stuff.</p> <pre><code>#include &lt;iostream&gt; template &lt;unsigned long long N, unsigned int P, int Odd = (P&amp;1)&gt; struct Power; template &lt;unsigned long long N, unsigned int P&gt; struct Power&lt;N,P,0&gt; { // even (square N and halve the power) static const unsigned long long val = Power&lt;N*N,(P/2)&gt;::val; }; template &lt;unsigned long long N, unsigned int P&gt; struct Power&lt;N,P,1&gt; { // odd (multiply by N and decrement the power) static const unsigned long long val = N * Power&lt;N,(P-1)&gt;::val; }; template &lt;unsigned long long N&gt; struct Power&lt;N,0,0&gt; { // zero (x**0 is 1 for all x != 0) static const unsigned long long val = 1; }; int main() { std::cout &lt;&lt; "2**0 = " &lt;&lt; Power&lt;2,0&gt;::val &lt;&lt; "\n"; std::cout &lt;&lt; "2**1 = " &lt;&lt; Power&lt;2,1&gt;::val &lt;&lt; "\n"; std::cout &lt;&lt; "2**2 = " &lt;&lt; Power&lt;2,2&gt;::val &lt;&lt; "\n"; std::cout &lt;&lt; "2**3 = " &lt;&lt; Power&lt;2,3&gt;::val &lt;&lt; "\n"; std::cout &lt;&lt; "2**4 = " &lt;&lt; Power&lt;2,4&gt;::val &lt;&lt; "\n"; std::cout &lt;&lt; "2**5 = " &lt;&lt; Power&lt;2,5&gt;::val &lt;&lt; "\n"; std::cout &lt;&lt; "2**6 = " &lt;&lt; Power&lt;2,6&gt;::val &lt;&lt; "\n"; std::cout &lt;&lt; "2**7 = " &lt;&lt; Power&lt;2,7&gt;::val &lt;&lt; "\n"; std::cout &lt;&lt; "2**8 = " &lt;&lt; Power&lt;2,8&gt;::val &lt;&lt; "\n"; std::cout &lt;&lt; "2**9 = " &lt;&lt; Power&lt;2,9&gt;::val &lt;&lt; "\n"; std::cout &lt;&lt; "2**10 = " &lt;&lt; Power&lt;2,10&gt;::val &lt;&lt; "\n"; std::cout &lt;&lt; "2**11 = " &lt;&lt; Power&lt;2,11&gt;::val &lt;&lt; "\n"; std::cout &lt;&lt; "2**12 = " &lt;&lt; Power&lt;2,12&gt;::val &lt;&lt; "\n"; std::cout &lt;&lt; "2**30 = " &lt;&lt; Power&lt;2,30&gt;::val &lt;&lt; "\n"; std::cout &lt;&lt; "2**40 = " &lt;&lt; Power&lt;2,40&gt;::val &lt;&lt; "\n"; std::cout &lt;&lt; "2**50 = " &lt;&lt; Power&lt;2,50&gt;::val &lt;&lt; "\n"; std::cout &lt;&lt; "2**60 = " &lt;&lt; Power&lt;2,60&gt;::val &lt;&lt; "\n"; return 0; } </code></pre> <p>Disclaimer: I make no claims that this code will necessarily compile faster due to the reduction in number of template instantiations (though it might). I really only wrote it as a toy demo. I leave it as an exercise for the reader to write a version that isn't vulnerable to someone explicitly passing a value for the <code>Odd</code> parameter when using <code>Power&lt;&gt;</code>.</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