As I understand it, given the following code:
template<int N>
int i = i<N - 1> + i<N - 1>; // primary variable template
template<> int i<0> = 1; // and explicit specialization
the following program must return 2:
int main () { return i<1>; }
but this program can return 0 or 4:
int main () { return i<2>; }
since evaluating the instantiation of i<2>
gives:
int i<2> = i<1> + i<1>; // generated by the compiler
Here, since i<1>
is only implicitly instantiated, the compiler can use either the statically initialized value of i<1>
, which is 0
, or the dynamically initialized value, which is 2
.
Here's a comparison of different compilers giving different outputs (clang returns 0, gcc returns 4).
The question is - can this program return 2?
int main () { return i<2>; }
i.e. in the initialization of i<2>
, can different initializations be used for the different uses of i<1>
? Can the program return some other value entirely?