Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The Standard is very clear on this:</p> <blockquote> <p><strong>14.7.1 Implicit instantiation [temp.inst]</strong></p> <p>9 The implicit instantiation of a class template does not cause any static data members of that class to be implicitly instantiated.</p> </blockquote> <p>All the calls in <code>main()</code> to your <code>Fibo&lt;n&gt;::x</code> for <code>n &gt; 1</code>, are explicit instantiations, that through the Fibonnaci recursion will implicitly instantiate <code>Fibo&lt;n-1&gt;</code> and <code>Fibo&lt;n-2&gt;</code> but not their members <code>x</code>. This means that at those points, the <code>static</code> members <code>x</code> will be evaluated to their default initialization of <code>0</code>. For <code>n=1</code> and <code>n=0</code>, the compiler will see the explicit initialization values of 1. So effectively, you get the following computation</p> <pre><code>Fibo&lt;5&gt;::x --&gt; Fibo&lt;4&gt;::x + Fibo&lt;3&gt;::x --&gt; 0 + 0 = 0 Fibo&lt;4&gt;::x --&gt; Fibo&lt;3&gt;::x + Fibo&lt;2&gt;::x --&gt; 0 + 0 = 0 Fibo&lt;3&gt;::x --&gt; Fibo&lt;2&gt;::x + Fibo&lt;1&gt;::x --&gt; 0 + 1 = 1 Fibo&lt;2&gt;::x --&gt; Fibo&lt;1&gt;::x + Fibo&lt;0&gt;::x --&gt; 1 + 1 = 2 Fibo&lt;1&gt;::x --&gt; 1 Fibo&lt;0&gt;::x --&gt; 1 </code></pre> <p>You need to instantiate the static member <code>x</code> before evaluating the Fibonacci recursion. You can do this through a <code>static const int</code> or <code>enum</code> member <code>x</code>, or through a function (possibly <code>constexpr</code> in C++11) as shown by @Jarod42.</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