views:

116

answers:

2

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 ?

A: 

No constructor is applied at compile. In fact, no code at all is executing at compile time.

Franci Penov
+2  A: 

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

class One{
 public:
  int adad1;
  int adad2;
};

// initialized statically, if a and b are constant-expressions
One one = { a, b }; 

As another guy says, constexpr in 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/2

An implementation is permitted to perform the initialization of an object of namespace scope with static storage duration as a static initialization even if such initialization is not required to be done statically, provided that

  • the dynamic version of the initialization does not change the value of any other object of namespace scope with static storage duration prior to its initialization, and
  • the static version of the initialization produces the same value in the initialized object as would be produced by the dynamic initialization if all objects not required to be initialized statically were initialized dynamically.

If 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

inline double fd() { return 1.0; }
extern double d1;
double d2 = d1;   // unspecified:
                  // may be statically initialized to 0.0 or
                  // dynamically initialized to 1.0

double d1 = fd(); // may be initialized statically to 1.0

As you see, putting things to be initialized earlier can even go with changed initial values if there exist a certain relation between variables.

Johannes Schaub - litb
very nice description, but why did you say at first with certainty that none of cases can be done at compile time if the rules may still allow that ?
Pooria
@Pooria you don't know whether the implementation optimizes it. I meant to say you can't rely on it (so you have to assume you get static initialization order problems)
Johannes Schaub - litb
so please edit your first sentence to solve ambiguity.
Pooria
@Johannes Schaub - litb: After all, I doubt that second case is accepted by that standard because that involves some codes regarding for loop and some stuff; first case could be because it has an empty body function so that it can be converted to a constant expression, what do you say ?.
Pooria
@Pooria it doesn't matter what the body contains. Loops or anything else can be optimized out by the compiler if it's good enough. I doubt the the compiler does it, but it's allowed to. Unless you have "volatile" variables, but in that code there is no "volatile" in sight :)
Johannes Schaub - litb
@Johannes Schaub - litb: can you tell where in the standard it's told that loops or anything can be optimized away, cause that sounds like a UFO to me !!!
Pooria
@Pooria it's pretty standard to do that. Loop unrolling, constant propagation, inlining (even whole function calls are optimized away that way). It's specified that this is valid in verses of subsection 1.9 in the Standard.
Johannes Schaub - litb
@Johannes Schaub - litb: oh my !!! never thought there are such optimizations.
Pooria