Jump to page: 1 2
Thread overview
Re: Cannot alias null
Jun 12, 2014
H. S. Teoh
Jun 12, 2014
monarch_dodra
Jun 12, 2014
Ali Çehreli
Jun 13, 2014
monarch_dodra
Jun 12, 2014
Tom Browder
Jun 12, 2014
Ali Çehreli
Jun 12, 2014
Tom Browder
Jun 13, 2014
Marc Schütz
Jun 13, 2014
Tom Browder
Jun 13, 2014
Philpax
Jun 13, 2014
monarch_dodra
Jun 13, 2014
Tom Browder
June 12, 2014
On Thu, Jun 12, 2014 at 03:26:13PM -0500, Tom Browder via Digitalmars-d-learn wrote:
> This will not compile:
> 
>   alias blah = null;
[...]

'null' is a value, not a type. Try:

	alias blah = typeof(null);


T

-- 
If it's green, it's biology, If it stinks, it's chemistry, If it has numbers it's math, If it doesn't work, it's technology.
June 12, 2014
On Thu, Jun 12, 2014 at 3:42 PM, H. S. Teoh via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:
> On Thu, Jun 12, 2014 at 03:26:13PM -0500, Tom Browder via Digitalmars-d-learn wrote:
>> This will not compile:
>>
>>   alias blah = null;
> [...]
>
> 'null' is a value, not a type. Try:
>
>         alias blah = typeof(null);

Great, that works!

What I was really trying to do was D'ify C expressions like this:

  typedef ((struct t*)0) blah;

So, taking your advice, I found this to work (at least it compiles as a translation:

  alias blah = typeof(null);

Thanks,T and Ali.

Best,

-Tom
June 12, 2014
On 06/12/2014 02:06 PM, Tom Browder via Digitalmars-d-learn wrote:

> What I was really trying to do was D'ify C expressions like this:
>
>    typedef ((struct t*)0) blah;

Is that actually a function pointer typedef? I can't parse that line. :)

> So, taking your advice, I found this to work (at least it compiles as
> a translation:
>
>    alias blah = typeof(null);

I suspect you need something else. :)

Ali

June 12, 2014
On Thu, Jun 12, 2014 at 4:17 PM, Ali Çehreli <digitalmars-d-learn@puremagic.com> wrote:
> On 06/12/2014 02:06 PM, Tom Browder via Digitalmars-d-learn wrote:
...
>> What I was really trying to do was D'ify C expressions like this:
>>
>>    typedef ((struct t*)0) blah;
...
>> So, taking your advice, I found this to work (at least it compiles as a translation:
>>
>>    alias blah = typeof(null);
>
> I suspect you need something else. :)

Undoubtedly, indeed!  [Still a WIP (work in progress).]

Best,

-Tom

June 12, 2014
On Thursday, 12 June 2014 at 20:44:16 UTC, H. S. Teoh via Digitalmars-d-learn wrote:
> On Thu, Jun 12, 2014 at 03:26:13PM -0500, Tom Browder via Digitalmars-d-learn wrote:
>> This will not compile:
>> 
>>   alias blah = null;
> [...]
>
> 'null' is a value, not a type. Try:
>
> 	alias blah = typeof(null);
>
>
> T

Yet you can alias variables...

int i;
alias j = i;

So there's something special about "null".
June 12, 2014
On 06/12/2014 03:38 PM, monarch_dodra wrote:

> Yet you can alias variables...
>
> int i;
> alias j = i;

Initially I forgot about the fact that symbols can be alias'ed as well. So that's fine.

> So there's something special about "null".

The difference is that null is an expression. It is the same limitation as not being able to alias a literal.

    alias zero = 0;
    alias blah = null;

Those two declarations fail for the same reason:

  Error: basic type expected, not 0
  Error: semicolon expected to close alias declaration
  Error: basic type expected, not null
  Error: semicolon expected to close alias declaration

The pair of error messages are somewhat silly: The first one is misleading because as we know, it should say "basic type *or symbol* expected"; and the second one is bogus because there actually is a semicolon there: :p

Ali

June 13, 2014
On Thursday, 12 June 2014 at 21:07:47 UTC, Tom Browder via Digitalmars-d-learn wrote:
> What I was really trying to do was D'ify C expressions like this:
>
>   typedef ((struct t*)0) blah;

This doesn't compile for me with GCC, and I don't know what it's supposed to mean. ((struct t*) 0) is a value, not a type...

Where does it come from?
June 13, 2014
On Thursday, 12 June 2014 at 22:54:20 UTC, Ali Çehreli wrote:
> On 06/12/2014 03:38 PM, monarch_dodra wrote:
> > So there's something special about "null".
>
> The difference is that null is an expression. It is the same limitation as not being able to alias a literal.
>
>     alias zero = 0;
>     alias blah = null;

Oh! Right. That makes sense.

So you should use enum instead:
enum zero = 0;
enum blah = null;

Thanks.
June 13, 2014
On Fri, Jun 13, 2014 at 7:59 AM, via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:
> On Thursday, 12 June 2014 at 21:07:47 UTC, Tom Browder via Digitalmars-d-learn wrote:
>>
>> What I was really trying to do was D'ify C expressions like this:
>>
>>   typedef ((struct t*)0) blah;
>
>
> This doesn't compile for me with GCC, and I don't know what it's supposed to
> mean. ((struct t*) 0) is a value, not a type...

Sorry, you're correct.  It is from a C macro and would be used for an rvalue.  Something like this:

$ cat chdr.h
struct t;
#define t_nullptr ((struct t*)0)
struct t* t_ptr = t_nullptr;

After pre-processing with "gcc -E -P" that should read:

$ cat chdr.h.i
struct t;
struct t* t_ptr = ((struct t*)0);

which does compile.

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;

> Where does it come from?

The usage comes from many of the C API headers in the BRL-CAD package
(http://brlcad.org).

Best,

-Tom
June 13, 2014
On Friday, 13 June 2014 at 15:05:49 UTC, Tom Browder via Digitalmars-d-learn wrote:
> On Fri, Jun 13, 2014 at 7:59 AM, via Digitalmars-d-learn
> <digitalmars-d-learn@puremagic.com> wrote:
>> On Thursday, 12 June 2014 at 21:07:47 UTC, Tom Browder via
>> Digitalmars-d-learn wrote:
>>>
>>> What I was really trying to do was D'ify C expressions like this:
>>>
>>>   typedef ((struct t*)0) blah;
>>
>>
>> This doesn't compile for me with GCC, and I don't know what it's supposed to
>> mean. ((struct t*) 0) is a value, not a type...
>
> Sorry, you're correct.  It is from a C macro and would be used for an
> rvalue.  Something like this:
>
> $ cat chdr.h
> struct t;
> #define t_nullptr ((struct t*)0)
> struct t* t_ptr = t_nullptr;
>
> After pre-processing with "gcc -E -P" that should read:
>
> $ cat chdr.h.i
> struct t;
> struct t* t_ptr = ((struct t*)0);
>
> which does compile.
>
> 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;
>
>> Where does it come from?
>
> The usage comes from many of the C API headers in the BRL-CAD package
> (http://brlcad.org).
>
> Best,
>
> -Tom

Remove the struct from the pointer:

struct t;
t* t_ptr = null;
« First   ‹ Prev
1 2