<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<TITLE>
    CWG Issue 362</TITLE>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<STYLE TYPE="text/css">
  INS { text-decoration:none; font-weight:bold; background-color:#A0FFA0 }
  .INS { text-decoration:none; background-color:#D0FFD0 }
  DEL { text-decoration:line-through; background-color:#FFA0A0 }
  .DEL { text-decoration:line-through; background-color: #FFD0D0 }
  @media (prefers-color-scheme: dark) {
    HTML { background-color:#202020; color:#f0f0f0; }
    A { color:#5bc0ff; }
    A:visited { color:#c6a8ff; }
    A:hover, a:focus { color:#afd7ff; }
    INS { background-color:#033a16; color:#aff5b4; }
    .INS { background-color: #033a16; }
    DEL { background-color:#67060c; color:#ffdcd7; }
    .DEL { background-color:#67060c; }
  }
  SPAN.cmnt { font-family:Times; font-style:italic }
</STYLE>
</HEAD>
<BODY>
<P><EM>This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21
  Core Issues List revision 118b.
  See http://www.open-std.org/jtc1/sc22/wg21/ for the official
  list.</EM></P>
<P>2025-09-28</P>
<HR>
<A NAME="362"></A><H4>362.
  
Order of initialization in instantiation units
</H4>
<B>Section: </B>5.2&#160; [<A href="https://wg21.link/lex.phases">lex.phases</A>]
 &#160;&#160;&#160;

 <B>Status: </B>CD1
 &#160;&#160;&#160;

 <B>Submitter: </B>Mark Mitchell
 &#160;&#160;&#160;

 <B>Date: </B>2 July 2002<BR>


<P>[Voted into WP at March 2004 meeting.]</P>

<P>Should this program do what its author obviously expects?  As far as
I can tell, the standard says that the point of instantiation for
Fib&lt;n-1&gt;::Value is the same as the point of instantiation as the
enclosing specialization, i.e., Fib&lt;n&gt;::Value.  What in the standard
actually says that these things get initialized in the right order?</P>
<PRE>
  template&lt;int n&gt;
  struct Fib { static int Value; };

  template &lt;&gt;
  int Fib&lt;0&gt;::Value = 0;

  template &lt;&gt;
  int Fib&lt;1&gt;::Value = 1;

  template&lt;int n&gt;
  int Fib&lt;n&gt;::Value = Fib&lt;n-1&gt;::Value + Fib&lt;n-2&gt;::Value;

  int f ()
  {
    return Fib&lt;40&gt;::Value;
  }
</PRE>
<P>
<U>John Spicer</U>:
My opinion is that the standard does not specify
the behavior of this program.
I thought there was a core issue related to this, but I could not find it.
The issue that I recall proposed tightening up the static initialization
rules to make more cases well defined.</P>

<P>Your comment about point of instantiation is correct, but I don't think that
really matters.  What matters is the order of execution of the initialization
code at execution time.  Instantiations don't really live in
"translation units"
according to the standard.  They live in "instantiation units", and the
handling of instantiation units in initialization is unspecified (which should
probably be another core issue).  See 5.2 [<A href="https://wg21.link/lex.phases#8">lex.phases</A>] paragraph 8.
</P>

<P><B>Notes from October 2002 meeting:</B></P>

<P>We discussed this and agreed that we really do mean the the order
is unspecified.  John Spicer will propose wording on handling of
instantiation units in initialization.</P>

<P><B>Proposed resolution (April 2003):</B></P>
<P>
TC1 contains the following text in 6.10.3.2 [<A href="https://wg21.link/basic.start.static#1">basic.start.static</A>] paragraph 1:
</P>

<BLOCKQUOTE>
Objects with static storage duration defined in namespace scope in the
same translation unit and dynamically initialized shall be initialized
in the order in which their definition appears in the translation
unit.
</BLOCKQUOTE>

<P>
This was revised by <A HREF="270.html">issue 270</A> to read:</P>

<BLOCKQUOTE>
Dynamic initialization of an object is either ordered or unordered.
Explicit specializations and definitions
of class template static data members have ordered
initialization. Other class template static data member instances have
unordered initialization. Other objects defined in namespace scope
have ordered initialization. Objects defined within a single
translation unit and with ordered initialization shall be initialized
in the order of their definitions in the translation unit. The order
of initialization is unspecified for objects with unordered
initialization and for objects defined in different translation units.
</BLOCKQUOTE>

<P>
This addresses this issue but while reviewing this issue some
additional changes were suggested for the above wording:</P>

<BLOCKQUOTE>
Dynamic initialization of an object is either ordered or unordered.
<INS>Definitions of explicitly specialized</INS>
<DEL>Explicit specializations and definitions of</DEL>
class template static data members have ordered
initialization. Other class template static data member<INS>s (i.e.,
implicitly or explicitly instantiated specializations)</INS>
<DEL>instances</DEL> have
unordered initialization. Other objects defined in namespace scope
have ordered initialization. Objects defined within a single
translation unit and with ordered initialization shall be initialized
in the order of their definitions in the translation unit. The order
of initialization is unspecified for objects with unordered
initialization and for objects defined in different translation units.
</BLOCKQUOTE>

<BR><BR>
</BODY>
</HTML>
