title: Order of static initialization with function call
body: <p>I have a static global variable initialized with a function call. This causes the initialization order to be not respected, eve inside a translation unit. This is a reproducer (the order of the code makes little sense here but it resembles the original arrangement in my use case, and is important to trigger the issue, so please disregard it):</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;array&gt;
  #include &lt;iostream&gt;
  #include &lt;limits&gt;
  
  template &lt;typename T&gt; constexpr T defaultValue = std::numeric_limits&lt;T&gt;::max();
  
  namespace _Private {
  template &lt;typename T&gt; std::array&lt;T, 3&gt; getDefaultArrayValue() {
    std::cout &lt;&lt; &quot;Initialize array&quot; &lt;&lt; std::endl;
    std::array&lt;T, 3&gt; result;
    result[0] = defaultValue&lt;T&gt;;
    result[1] = defaultValue&lt;T&gt;;
    result[2] = defaultValue&lt;T&gt;;
    return result;
  }
  } // namespace _Private
  
  // This generates the wrong behavior
  template &lt;typename T&gt; const std::array&lt;T, 3&gt; defaultValue&lt;std::array&lt;T, 3&gt;&gt; = _Private::getDefaultArrayValue&lt;T&gt;();
  // This behaves right
  //template &lt;typename T&gt; const std::array&lt;T, 3&gt; defaultValue&lt;std::array&lt;T, 3&gt;&gt; = {defaultValue&lt;T&gt;, defaultValue&lt;T&gt;, defaultValue&lt;T&gt;};
  
  struct TestClass {
    std::array&lt;float, 3&gt; arr;
    TestClass();
  };
  
  TestClass tc;
  
  int main() {
  
    std::cout &lt;&lt; tc.arr[0] &lt;&lt; std::endl;
  
    return 0;
  }
  
  TestClass::TestClass() : arr{defaultValue&lt;std::array&lt;float, 3&gt;&gt;} {std::cout &lt;&lt; &quot;Build TestClass&quot; &lt;&lt; std::endl;}
  </code></pre>
  <p>Here I have a <code>TestClass</code> class whose constructor initializes the <code>arr</code> member variable to its default value, as defined by the <code>defaultValue&lt;std::array&lt;float,3&gt;&gt;</code> specialization; the latter is a global variable whose value is set equal to the return value of <code>getDefaultArrayValue()</code>. A global instance of <code>TestClass</code> is created, and I would expect that its <code>arr</code> member is correctly initialized to the specified default, that is 3.40282e+38 (i.e. the maximum float value). However, running the program the value 0. is printed, because the initialization of <code>defaultValue&lt;std::array&lt;float, 3&gt;&gt;</code> is actually done after the creation of <code>tc</code>:</p>
  <pre><code>$ ./a.out 
  Build TestClass
  Initialize array
  0
  </code></pre>
  <p>so when <code>tc</code> is built <code>defaultValue&lt;std::array&lt;float, 3&gt;&gt;</code> still does not have its correct value. The problem is fixed if <code>defaultValue&lt;std::array&lt;float, 3&gt;&gt;</code> is initialized by directly assigning a value to it (see the commented line in the reproducer code) rather than calling a function, restoring the &quot;from first to last&quot; static variable initialization order rule that applies to a single translation unit.</p>
  <p>To summarize, it seems that calling a function to initialize a global variable breaks the initialization order. Is this behavior mandated by the standard or is it an implementation detail (I'm using GCC 11.2.0)?</p>