Usenet.com

www.Usenet.com

Group Index

Comp Thread Archive from Usenet.com

<-- __Chronological__ --> <-- __Thread__ -->

Re: 362. Order of initialization in instantiation unit



Ben Hutchings wrote:

> Markus Werle wrote:
>> Hi!
>> 
>> In
>> http://std.dkuug.dk/JTC1/SC22/WG21/docs/cwg_active.html
>> Issue 362:
>> 
>>  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;
>>   }
>> 
>> IIUC, the code above has undefined behaviour.
>> Did I understand this correctly?
> 
> The issue is that the order of initialisation of the Value members
> of Fib<n> for n > 1 is unspecified (not undefined) so the result of
> f() might not be the 40th Fibonacci number.

I do not understand.
This calculation normally takes place at compile time, 
how can any order issues affect the result? 
Please explain in detail.

Is there a workaround?


> Quite apart from this, the code has undefined behaviour if INT_MAX
> is less than the intended result

OK.

> or if the number of recursively
> nested template instantiations is less than 39. 

39? _less_ than? Wasn't it more than 18?

> This doesn't seem to be a very good example!

It is. The idea is straightforward.
The Expression Template world is full of papers which assume
the code above to work. I need to really understand this.

Markus

-- 

Build your own Expression Template Library with Daixtrose!
Visit http://daixtrose.sourceforge.net/ 

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]



<-- __Chronological__ --> <-- __Thread__ -->


Usenet.com



Please check out one of the premium Usenet Newsgroup Service Providers below for access to Usenet.