Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Static member initialization (template fun inside)
    text
    copied!<p>For static member initialization I use a nested helper struct, which works fine for non templated classes. However, if the enclosing class is parameterized by a template, the nested initialization class is not instantiated, if the helper object is not accessed in the main code. For illustration, a simplified example (In my case, I need to initialize a vector). </p> <pre><code>#include &lt;string&gt; #include &lt;iostream&gt; struct A { struct InitHelper { InitHelper() { A::mA = "Hello, I'm A."; } }; static std::string mA; static InitHelper mInit; static const std::string&amp; getA(){ return mA; } }; std::string A::mA; A::InitHelper A::mInit; template&lt;class T&gt; struct B { struct InitHelper { InitHelper() { B&lt;T&gt;::mB = "Hello, I'm B."; // [3] } }; static std::string mB; static InitHelper mInit; static const std::string&amp; getB() { return mB; } static InitHelper&amp; getHelper(){ return mInit; } }; template&lt;class T&gt; std::string B&lt;T&gt;::mB; //[4] template&lt;class T&gt; typename B&lt;T&gt;::InitHelper B&lt;T&gt;::mInit; int main(int argc, char* argv[]) { std::cout &lt;&lt; "A = " &lt;&lt; A::getA() &lt;&lt; std::endl; // std::cout &lt;&lt; "B = " &lt;&lt; B&lt;int&gt;::getB() &lt;&lt; std::endl; // [1] // B&lt;int&gt;::getHelper(); // [2] } </code></pre> <p>With g++ 4.4.1: </p> <ul> <li><p>[1] and [2] commented: </p> <pre>A = Hello, I'm A.</pre> <p>Works as intended</p></li> <li><p>[1] uncommented: </p> <pre>A = Hello, I'm A. B = </pre> <p>I would expect, that the InitHelper initializes mB</p></li> <li>[1] and [2] uncommented: <pre>A = Hello, I'm A. B = Hello, I'm B.</pre> Works as intended</li> <li>[1] commented, [2] uncommented:<br> Segfault in the static initialization stage at [3] </li> </ul> <p>Thus my question: Is this a compiler bug or is the bug sitting between the monitor and the chair? And if the latter is the case: Is there an elegant solution (i.e. without explicitly calling a static initialization method)?</p> <p><strong>Update I:</strong><br> This seems to be a desired behavior (as defined in the ISO/IEC C++ 2003 standard, 14.7.1):</p> <blockquote> <p>Unless a member of a class template or a member template has been explicitly instantiated or explicitly specialized, the specialization of the member is implicitly instantiated when the specialization is referenced in a context that requires the member definition to exist; in particular, the initialization (and any associated side-effects) of a static data member does not occur unless the static data member is itself used in a way that requires the definition of the static data member to exist.</p> </blockquote>
 

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