Dec 30, 2010 at 10:13pm UTC
Does the following initialize the FIB array during compilation?
If not, how would I go about doing it?
If yes, is there a better way?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <iostream>
int FIB[21];
template <int N> struct Fib { static const int VAL; };
template <int N> const int Fib<N>::VAL=FIB[N] =Fib<N-1>::VAL+Fib<N-2>::VAL;
template <> struct Fib<1> { static const int VAL; };
const int Fib<1>::VAL=FIB[1] =1;
template <> struct Fib<2> { static const int VAL; };
const int Fib<2>::VAL=FIB[2] =1;
template struct Fib<20>; //explicit template instantiation
int main()
{
for (int i=1; i<=20; i++)
std::cout << FIB[i] << std::endl;
std::cout << "\nhit enter to quit..." ;
std::cin.get();
return 0;
}
Last edited on Dec 31, 2010 at 1:11am UTC
Dec 30, 2010 at 11:44pm UTC
guestgulkan wrote:Bugger, you've changed your code.
Hahaha :D
I just removed
Fib<20>::VAL;
from inside main and placed
template struct Fib<20>;
above it.
Last edited on Dec 31, 2010 at 12:01am UTC
Dec 30, 2010 at 11:51pm UTC
I would say at compile time - but I'm prepared to stand corrected.
Last edited on Dec 30, 2010 at 11:58pm UTC
Dec 31, 2010 at 12:01am UTC
Okay, let's see what other people have to say.
BTW, where did your other post go? I thought you couldn't delete a post if someone else posted after you.
Last edited on Dec 31, 2010 at 12:06am UTC
Dec 31, 2010 at 12:26am UTC
Hmmm... Yes, so it seems. Thanks for your answers.
Dec 31, 2010 at 8:47am UTC
Furthermore looking into the line noise that
gave me, I found this over and over again:
1 2 3 4 5 6 7 8
L17:
cmpb $0, _ZGVN3FibILi19EE3VALE
jne .L18
movl _ZN3FibILi17EE3VALE, %eax
addl _ZN3FibILi18EE3VALE, %eax
movb $1, _ZGVN3FibILi19EE3VALE
movl %eax, FIB+76
movl %eax, _ZN3FibILi19EE3VALE
Your template is not being turned into data, it is being turned into a ream of *code* (quite inefficient code I may add) which is executed at runtime. Although in theory it would be nice if the compiler was an interpreter for the bizarre template language, it is not. In fact, looking at this mess shows that the compiler is having a hard enough time *parsing* the correct meaning of your runes.
Last edited on Dec 31, 2010 at 11:08am UTC
Dec 31, 2010 at 9:08am UTC
As for the questions "If not, how would I go about doing it? If yes, is there a better way?", the way *I* would do this is to generate the code using a *real* programming language, in this case Perl.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#!/usr/bin/perl
$code = <<'EOT' ;
#include "iostream"
int fib[20]={
EOT
$a=1;
$b=1;
$c=0;
while ($c++<20){
$code .= "$a," ;
$a+=$b;
($a,$b)=($b,$a);
}
chop $code;
$code .= <<'EOT' ;
};
int main()
{
for (int i=0; i<20; i++)
std::cout << fib[i] << std::endl;
std::cout << "\nhit enter to quit..." ;
std::cin.get();
return 0;
}
EOT
srand;
open FILE, "> orenfib.cpp" ;
print FILE $code;
edit- small error of array indices.
Last edited on Dec 31, 2010 at 1:39pm UTC
Dec 31, 2010 at 2:21pm UTC
That's very interesting. Thanks.