More on C++ stack arrays

Jonathan M Davis jmdavisProg at gmx.com
Wed Oct 23 12:49:24 PDT 2013


On Wednesday, October 23, 2013 19:28:27 deadalnix wrote:
> On Wednesday, 23 October 2013 at 14:54:22 UTC, dennis luehring
> 
> wrote:
> > can't you remove the if(this.ptr is null) return; checks
> > everywhere - how should that happen - without exception at
> > creation time
> 
> Struct.init must be a valid state according to D specs, and it is
> pretty much unavoidable considering we have no default
> constructor for structs.

And what do you mean by valid? It's perfectly legal to have fields initialized 
to void so that the init state is effectively garbage. That can cause problems 
in some scenarios (particularly any case where something assumes that init is 
useable without calling a function which would make the state valid), but it's 
legal. And you can disable init if you want to - which also causes its own set 
of problems, but technically, you don't even have to have an init value 
(though it can certainly be restrictive if you don't - particularly when 
arrays get involved).

You also have cases where the struct's init is in a completely valid and yet 
unusable state. For instance, SysTime.init is useless unless you set its 
timezone and will segfault if you try and use it (since the timezone is null), 
but thanks to the limitations of CTFE, you _can't_ have a fully valid 
SysTime.init (though that's not invalid in the sense that part of the struct 
is garbage - just that it blows up when you use it).

I don't know why you think that the spec requires that a struct's init value 
be valid. It just causes issues with some uses of the struct if its init value 
isn't valid.

- Jonathan M Davis


More information about the Digitalmars-d mailing list