struct members not default initialized?
Ali Cehreli
acehreli at yahoo.com
Mon Dec 7 17:35:18 PST 2009
Ali Cehreli Wrote:
> (I must be missing something here
Yes I am! :)
> I've been under the impression that struct members would always be initialized. Having seen D an init-happy language compared to C and C++, that's what I would have expected. :)
I still think that it should be the default behavior unless no-initialization was specifically requested, like the case for uninitialized arrays.
> struct S
> {
> int i;
> double d;
> }
[...]
> Also, leaving out some of the initializers do not init the corresponding members either:
>
> S s = { 42 };
>
> There, s.d is not double.init.
The spec says "Members not specified in the initializer list are default initialized" for *static* objects. So this works:
static S s = { 42 };
dout.writefln(s.d); // prints nan
This check fails though:
assert(s.d == double.nan);
and it is probably not meant to work anyway; because I see that the spec at
http://www.digitalmars.com/d/2.0/float.html
has this to say (probably recently modified):
x == x â true not valid if x is a NaN
> struct S
> {
> int i = 1;
> double d = 2.3;
> }
>
> S s = { 42 };
Good: That does set s.d... :)
Ali
More information about the Digitalmars-d-learn
mailing list