This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 119a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.
2025-12-20
[Voted into WP at March 2004 meeting.]
Should this program do what its author obviously expects? As far asI can tell, the standard says that the point of instantiation forFib<n-1>::Value is the same as the point of instantiation as theenclosing specialization, i.e., Fib<n>::Value. What in the standardactually says that these things get initialized in the right order?
template<int n> struct Fib { static int Value; }; template <> int Fib<0>::Value = 0; template <> int Fib<1>::Value = 1; template<int n> int Fib<n>::Value = Fib<n-1>::Value + Fib<n-2>::Value; int f () { return Fib<40>::Value; }John Spicer:My opinion is that the standard does not specifythe 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 initializationrules to make more cases well defined.
Your comment about point of instantiation is correct, but I don't think thatreally matters. What matters is the order of execution of the initializationcode at execution time. Instantiations don't really live in"translation units"according to the standard. They live in "instantiation units", and thehandling of instantiation units in initialization is unspecified (which shouldprobably be another core issue). See 5.2 [lex.phases] paragraph 8.
Notes from October 2002 meeting:
We discussed this and agreed that we really do mean the the orderis unspecified. John Spicer will propose wording on handling ofinstantiation units in initialization.
Proposed resolution (April 2003):
TC1 contains the following text in 6.10.3.2 [basic.start.static] paragraph 1:
Objects with static storage duration defined in namespace scope in thesame translation unit and dynamically initialized shall be initializedin the order in which their definition appears in the translationunit.
This was revised byissue 270 to read:
Dynamic initialization of an object is either ordered or unordered.Explicit specializations and definitionsof class template static data members have orderedinitialization. Other class template static data member instances haveunordered initialization. Other objects defined in namespace scopehave ordered initialization. Objects defined within a singletranslation unit and with ordered initialization shall be initializedin the order of their definitions in the translation unit. The orderof initialization is unspecified for objects with unorderedinitialization and for objects defined in different translation units.
This addresses this issue but while reviewing this issue someadditional changes were suggested for the above wording:
Dynamic initialization of an object is either ordered or unordered.Definitions of explicitly specializedExplicit specializations and definitions ofclass template static data members have orderedinitialization. Other class template static data members (i.e.,implicitly or explicitly instantiated specializations)instanceshaveunordered initialization. Other objects defined in namespace scopehave ordered initialization. Objects defined within a singletranslation unit and with ordered initialization shall be initializedin the order of their definitions in the translation unit. The orderof initialization is unspecified for objects with unorderedinitialization and for objects defined in different translation units.