Note that there are some explanatory texts on larger screens.

plurals
  1. PODo typedefs of templates preserve static initialization order?
    text
    copied!<p>Within the same compilation unit, the C++ standard says that static initialization order is well defined -- it's the order of the declarations of the static objects. But using the Sun Studio 12 compiler I'm encountering unintuitive behavior. I've define a templated class <code>helper&lt;T&gt;</code> which contains a static member <code>_data</code> of type <code>T</code> and a static member function that uses <code>_data</code> called <code>foo</code>. In my .cpp file I have this above main():</p> <pre><code>struct A { /* some definition */ }; typedef helper&lt;int&gt; s0; typedef helper&lt;A&gt; s1; </code></pre> <p>Notice that the typedef for <code>helper&lt;int&gt;</code> comes <em>before</em> the typedef for <code>helper&lt;A&gt;</code>. Thus according to the standard I would expect that <code>helper&lt;int&gt;::_data</code> will be constructed before <code>helper&lt;A&gt;::_data</code> (remember <code>_data</code> is a static member). On GCC this is the case, on Sun it is not.</p> <p>This is problematic because A's constructor uses <code>helper&lt;int&gt;::_data</code>. I only have one compilation unit, with no earlier potential instantiation of <code>helper&lt;A&gt;</code>, so I thought the order should be well defined. Is this a Sun compiler bug, or does the typedef not constitute a definition/instantiation technically? What I mean is, is the Sun compiler's behavior allowed by the standard?</p> <p>I have the following main():</p> <pre><code>int main() { //Swapping the order of these has no effect on Sun s0::foo(); s1::foo(); } </code></pre> <p>There are no other uses of s0 or s1.</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