June 13, 2014 Re: Cannot alias null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tom Browder | 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.
|
June 13, 2014 Re: Cannot alias null | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | On Fri, Jun 13, 2014 at 10:15 AM, monarch_dodra via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > 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? I use "dmd -c .d" (note file name change) and get: t.d(2): Error: { } expected following aggregate declaration t.d(2): Error: Declaration expected, not '*' Taking Philpax's suggestion I try $ cat t.d struct t; t* t_ptr = null; $ dmd -c t.d and get a good build. > 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: ... > That said, I'd advise against this. Just use "null" and move on. It's idiomatic. So this is the correct (i.e., good enough) solution then: $ cat t.d struct t; t* t_ptr = null; Sounds good to me. Thanks all. Best, -Tom |
Copyright © 1999-2021 by the D Language Foundation