Cannot alias null

monarch_dodra via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 13 08:15:57 PDT 2014


On Friday, 13 June 2014 at 15:05:49 UTC, Tom Browder via 
Digitalmars-d-learn wrote:
> So I'm not sure how to translate that into D.   I do know my 
> first
> attempt here doesn't work, even with it being surrounded by 
> extern (C)
> {}:
>
> $ cat chdr.d
> struct t;
> struct t* t_ptr = null;

This seems to work fine for me. What's the problem?

Note this isn't *strictly* the same as what you have in C, as 
"null" is not strongly bindded to a single type. If you want a 
null pointer which is pre-emptivelly strongly, then you can use 
an eponymous template:

enum nullptr(T) = (T*).init;

auto p = nullptr!t;

Throw in an alias, and you got it working exactly like an C:


enum nullptr(T) = (T*).init;
alias t_nullptr = nullptr!t;
struct t* t_ptr = t_nullptr;

That said, I'd advise against this. Just use "null" and move on. 
It's idiomatic.


More information about the Digitalmars-d-learn mailing list