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 ?

Man.. you have some twisted questions..
I'm not sure of the rules. What I understand is, you mean what constructors will allow the compiler to build some constant values as literals. As in:

One some_variable(1,2); //would act like:
  int some_int(1);        //in the sense that the constructor is not called but the memory is initialized as part the initial stack frame of the function.

I would suspect, at least for the coming C++0x, that POD classes can be as such (look up POD classes (Plain Old Data)). Generally, anything that couldn't be a C struct is not a POD class. And in the case you have above non of them are POD because there are private data members. But I suspect, nevertheless, that compilers are clever enough to do a lot at compile time, even for complex classes.

But remember, this is a matter of micro-optimization, or even nano-optimization, which should never take precedence over code clarity and design. Don't let this issue determine how you program your application, because I can tell you that a constructor call is typically not within any CPU-bound looping algorithm, and thus, its speed is really not much of an issue.

Another thing, a function stack frame will be initialized as it is being cached, and the caching operation is usually much more expensive than any few class initialization you might have at the start of a function call.

mike_2000_17

Man.. you have some twisted questions..

Well, most of my questions are like that!!!:D

I can tell you that a constructor call is typically not within any CPU-bound looping algorithm, and thus, its speed is really not much of an issue.

I don't think that prevents compiler writers from doing optimizations.

One some_variable(1,2); //would act like:
  int some_int(1);        //in the sense that the constructor is not called but the memory is initialized as part the initial stack frame of the function.

Another thing, a function stack frame will be initialized as it is being cached, and the caching operation is usually much more expensive than any few class initialization you might have at the start of a function call.

Actually I'm talking about objects with static storage, so no stack frame is involved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.