take two following classes and their constructors as samples:
class One{
public:
One(int a,int b):adad1(a),adad2(b){}
private:
int adad1;
int adad2;
};
class Two{
public:
Two(int input[]){
for (int i=0;i<10;i++)
araye[i]=input[i];
}
private:
int araye[10];
};
considering objects with static storage duration, I think first constructor can be applied during compile time due to its free function body that allows it to be converted to a constant expression in some cases as an optimization, but I have doubt about second one. anyway is there any rule specifying which kinds of constructors can be applied during compile time ?
There is no guarantee that any of the two are statically initialized before any runtime code is executed. For the first, it’s easy to make it happen, though
As another guy says,
constexprin C++0x allows constructors to be executed statically. In your case that would work for the first case, but not for the second. You will have to live that for the second, no guarantee is made by the Standard. But the Standard still allows an implementation to optimize it to be done at static initialization phase. See 3.6.2/2If the array given is suitable, your constructor may not violate those rules. The Standard shows an example, which i explained in more detail here. For completion, the example code is shown below
As you see, putting things to be initialized earlier can even go with changed initial values if there exist a certain relation between variables.