comparing typedefed type to null

Jarrett Billingsley kb3ctd2 at yahoo.com
Fri Mar 2 08:24:23 PST 2007


"Rick Mann" <rmann-d-lang at latencyzero.com> wrote in message 
news:es7rp3$153q$1 at digitalmars.com...
> In writing code against the Mac OS X Carbon API, I often have a need to do 
> this:
>
> struct __Foo {};
> typedef Foo*   FooRef;
> typedef FooRef     BarRef;
>
> {
>  BarRef br = ...;
>  FooRef fr = ...;
>  if (br == null)  {} // complain
>  if (fr == null) {} ok
>
> GDC 0.22/DMD 1.004 complains about the comparison to br: "Error: 
> incompatible types for ((br) == (null)): 'BarRef' and 'void*'
>
> I can avoid it by aliasing BarRef instead of typedefing it, but I have the 
> need to allow a BarRef to be passed to FooRef parameters, but not the 
> other way around. The typedef gives me that.

template realType(T)
{
    static if(is(T U == typedef))
        alias realType!(U) realType;
    else
        alias T realType;
}

bool isNull(T)(T val)
{
    return (cast(realType!(T))val is null);
}

void main()
{
    BarRef br;
    FooRef fr;

    if (isNull(br)) {}
}



:S

There's got to be a better way.. 




More information about the Digitalmars-d-learn mailing list