== Quote from Jonathan M Davis (jmdavisp...@gmx.com)'s article > On Saturday 12 February 2011 10:48:04 Sean Eskapp wrote: > > With the removal of scope classes, the only way of which I know to use RAII > > objects is to use structs. This is fine, but structs aren't allowed default > > constructors! Why is this? > It's because they have to have an init property. _All_ types have an init > property. It's what they're default initialized to. For bool, it's false. For > integral types, it's 0. For floating point types, it's NAN. Etc. For classes, > that's null, so they can have default constructors just fine. However, for > structs, it must be what the struct's member variables are directly > initialized > to. If you could have an arbitrary default constructor, then you'd have the > problem of it throwing exceptions as well as it trying to run functions which > could only be run at runtime. The struct's init value must be known at compile > time, and it must _always_ be the same. So, it can't involve any functions > which > would have to be run at runtime, and it can't involve anything that would > result > in differing values betwen runs of the default constructor. > There has been some discussion of allowing structs to have limited default > constructors, but they would still disallow most of the kinds of things that > people would want to do with a default constructor. init _must_ be known at > compile time. So, its value is taken from the value that a struct gets when > all > of its member variables have been directly initialized (or default > initialized) > and no constructor has been called. > It _can_ be a bit annoying at times, but it's either that or allow undefined > struct values, which would be a big problem and go against D's philosophy on > default initialization. > Since you can't have a default constructor, what you do instead is define a > static opCall() function for the struct and use that. Then you construct a > struct (with the name S in this case) like so: > auto s = S(); > You're still stuck with the init state if you do > S s; > or if you're dealing with structs in an array and the like (since they're all > default constructed to the struct's init property). But you can do RAII just > fine. Personally, I _always_ use this syntax when declaring struct variables: > auto S = S(); > So, the situation isn't exactly ideal, but we're kind of stuck, given the > various constraints that we have to work with. > - Jonathan M Davis
Alright, thanks for clearing that up!