Why can't we make reference variables?

Namespace rswhite4 at googlemail.com
Wed Aug 29 08:49:23 PDT 2012


> *References cannot be null, whereas pointers can; every 
> reference refers to some object, although it may or may not be 
> valid. Note that for this reason, containers of references are 
> not allowed.
>
> * References cannot be uninitialized. Because it is impossible 
> to reinitialize a reference, they must be initialized as soon 
> as they are created. In particular, local and global variables 
> must be initialized where they are defined, and references 
> which are data members of class instances must be initialized 
> in the initializer list of the class's constructor. For example:
> int& k; // compiler will complain: error: `k' declared as 
> reference but not initialized

That would be a dream: not null references. I'm still think that 
D needs something like that. And I'm not talking about of some 
struct constructs like NotNullable, which will be added in 
std.typecons later. I'm talking about built-in support.


More information about the Digitalmars-d mailing list