Calculating an approximation for this piece of code e (i.e. mathematical continuation ~ 2.71828183) Is considered) using the following approach, at compile-time;
e1 = 2/1 e2 = (2 * 2 + 1) / (2 * 1) = 5/2 = 2.5 e3 = (3 * 5 + 1) / (3 * 2 ) = 16/6 ~ 2.67E4 = (4 * 16 + 1) / (4 * 6) = 65/24 ~ 2.708 ... E (I) = (E-1) .numer * i + 1 ) / (e (i-1) .denom * i)
computation of Prinam
is delivered through static member, however, after 2 Punravrittion Produces zero instead of expected value. I have added a stable member function F () which calculates the same value and which does not display the same problem.
#include & lt; Iostream & gt; # Include & lt; Iomanip & gt; // recurrence case template & lt; Int iters, int num = 2, int den = 1, int i = 2 & gt; Structure Kells (Stable Link Double Results; Static double (F) (Kailse & lt; Airs, New IM 1, DEN * i, i + 1 & gt; :: f ();}}; template & lt; int iters, int num, int den, int i & gt; Const double Kailse & lt; Aitrs, New York, Dan, I & gt; :: result = Kailse & lt; Aitrs number * I + 1, Dan * i, i + 1 & gt; :: results; / / Main case template structure kalase> lieutenant; ethers, pneum, dan, ethers & gt; {static const double result; static double f () {return result;} }; T Platte & lt; int iters, int num, int den; direct double calves & lt; ethers, moist, dane, etter & gt; :: result = static_account & gt; double & gt; (number) / den; / / with me? Ajhmao. Int main (int argc, char * argv []) {std :: cout & lt; & lt; Std :: setprecision (8); Std :: cout & lt; & lt; "e ~ 2" << Casey & Lt; 2 & gt; :: Results & lt; :: results & lt; & Lt; Std :: endl; Std :: cout & lt; & Lt; Std :: endl; Std :: cout & lt; & Lt; "E2 ~" & lt; & Lt; Calais and Lieutenant; 2 & gt; :: F () & lt; & Lt; Std :: endl; Std :: cout & lt; & Lt; "E3 ~" & lt; & Lt; Calais and Lieutenant; 3 & gt; :: F () & lt; & Lt; Std :: endl; Std :: cout & lt; & Lt; "E4 ~" & lt; & Lt; Calce & Lt; 4 & gt; :: F () & lt; & Lt; Std :: endl; Std :: cout & lt; & Lt; "E5 ~" & lt; & Lt; Calais and Lieutenant; 5> :: F () & lt; & Lt; Std :: endl; Return 0; }
I did it in VS 2008 and VS Tested with 2010, and obtained similar results in each case:
e2 ~ 2 e3 ~ 2.5 e4 ~ 0 e5 ~ 0 e2 ~ 2 e3 ~ 2.5 e4 ~ 2.6666667 e5 ~ 2.7083333
Why not return
do not give the expected value while f ()
does it?
According to the comment below, it works with GCC, so I think the question is, do I depend on some kind of undefined behavior in relation to the static initial order or whether it is visible in Visual Studio Is there a bug with?
Apparently you can not depend on the order of the station member, at least VC ++ In.
Here's a simple example:
#include & lt; Stdio.h & gt; Template & lt; Int N & gt; Structure A {static const int res; }; The template's & lt; & Gt; A structure & lt; 0 & gt; {Static const int res; }; Template & lt; Int N & gt; Const int a & lt; N & gt; :: res = a & lt; N-1 & gt; :: res; Const int a & lt; 0 & gt; :: res = 1; Int main () {printf ("% d \ n", a & lt; 3 & gt;: :); Printf ("% d \ n", a & lt; 2 & gt;: :); Printf ("% d \ n", a & lt; 1 & gt;: :); Printf ("% d \ n", a & lt; 0 & gt; :: res); }
In VC ++ 2008, it creates:
0 1 1
It creates:
1 1 1
Comments
Post a Comment