compile-time array initialization

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
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
I would say at compile time - but I'm prepared to stand corrected.
Last edited on
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
1
2
3
4
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;


If you don't do the specialization
template instantiation depth exceeds maximum of 
so I guess compilation time
Hmmm... Yes, so it seems. Thanks for your answers.
Actually, when I compile it with -S, it generates the symbol:
1
2
FIB:
        .zero   84

Which indicates that FIB is initialized at runtime.
m4ster r0shi wrote:
BTW, where did your other post go? I thought you couldn't delete a post if someone else posted after you.


I must have deleted it about the same time as you were writing your reply - bad timing I guess
Furthermore looking into the line noise that
g++ fibonacci.cpp -S -O3
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
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
That's very interesting. Thanks.
Topic archived. No new replies allowed.