I know that, in C++, when you write
int i;
you can not make any assumptions about the value that the variable will hold until you effectively assign it a value. However, if you write
int i = int();
then you have the guarantee that i
will be 0
. So my question is, isn't it actually an incosistency in the behavior of the language? I mean, if I have defined a class MyClass
and write
MyClass myInstance;
I can rest assured that the default constructor without parameters of the class will be called to initialize myInstance
(and the compiler will fail if there is none), because that's how the RAII principle goes. However, it seems that when it comes to primitive types, resource acquisition is not initialization anymore. Why is that?
I don't think that changing this behavior inherited from C would break any existing code (is there any code in the world that works on the assumption that no assumption can be made about the value of a variable?), so the main possible reason that comes to my mind is performance, for example when creating big arrays of primitive types; but still, I'd like to know if there is some official explanation to this.
Thanks.