Let's say I have a class A which contains a static const int array like the following.
class A {
static const int _array[];
static int fn( int n );
}
Function fn includes very heavy calculation. And now I want to initialize my static const array using the function fn.
I did that in the following way:
//.cpp file
int A::fn (int n){
....
return ....
}
const A::_array[] = {
fn(0);
fn(1);
fn(2);
...
fn(9);
}
My question is that whether the array initialization is in compile time? And how many times does the fn run if I use _array[i] in my other class methods? only 10 times in its initialization or it depends on how many times I use the _array?
EDIT: it was c++03 and is there any more efficient way to do this?
fn
can beconstexpr
?fn
has to perform user interaction, network IO and random number generation?const int A::_array[] = ...
in the cpp?