null [re: spec#]

Walter Bright newshound2 at digitalmars.com
Mon Nov 8 10:06:35 PST 2010


Jonathan M Davis wrote:
> On Monday, November 08, 2010 05:01:57 Simen kjaeraas wrote:
>> Jonathan M Davis <jmdavisProg at gmx.com> wrote:
>>> ??? Structs have no default constructor.
>> True. Disabling the default constructor here means that
>>
>> struct Foo{
>>      @disable this();
>> }
>> Foo f;
>>
>> should halt compilation at Foo f;, and certain other places where a
>> default constructor would normally be used.
> 
> I was not aware of that. I didn't think that you could do that for structs since 
> default constructors are illegal in the first place. It would likely have the 
> negative side effect of making it illegal to put Foo in arrays though, since it 
> wouldn't be possible to fill in the array values with init.

That's right.

The idea is to allow the declaration of a default constructor for structs, but 
only if it is marked as disabled. Then, any use of the struct that would require 
default initialization is disallowed. That includes static arrays, and arrays 
allocated via new.

A dynamic array could be constructed using a literal or by appending the values 
one by one.

This implies that a struct containing a field that has a disabled default 
constructor also cannot be default initialized.

Of course, you can defeat these protections by using a cast and encapsulating 
the code that does that. I believe Andrei was thinking of this when he mentioned 
using a template to initialize a nonnull array.


More information about the Digitalmars-d mailing list