Error: 'this' is only defined in non-static member functions, not main

remi thebault via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 29 14:33:14 PDT 2015


Hello

I have this weird error trying to achieve something simple:


module list_test;

// import wayland.util;


template Id(alias a) { alias Id = a; }


template ParentOf(alias member)
{
     alias ParentOf = Id!(__traits(parent, member));
}


template wl_container_of(alias member)
{
     ParentOf!(member)*
     get(wl_list* ptr)
     {
         return 
cast(ParentOf!(member)*)(cast(ptrdiff_t)(ptr)-member.offsetof);
     }
}


struct wl_list {
     wl_list *prev;
     wl_list *next;
}


struct item {
     int num;
     wl_list link;

     this(int num) {
         this.num = num;
     }
}

int main (string[] args)
{
     auto i1 = item(1);
     auto i2 = item(2);
     auto i3 = item(3);

     wl_list lst;
//     wl_list_init(&lst);
//     wl_list_insert(&lst, &i1.link);
//     wl_list_insert(&lst, &i2.link);
//     wl_list_insert(&i2.link, &i3.link);

     item *it = wl_container_of!(item.link).get(&i2.link); // 
error on this line

     return 0;
}


If I change the definition of wl_container_of to:
template wl_container_of(alias member)
{
     ParentOf!(member)*
     wl_container_of(wl_list* ptr)
     {
         return 
cast(ParentOf!(member)*)(cast(ptrdiff_t)(ptr)-member.offsetof);
     }
}

and call to:
wl_container_of!(item.link)(&i2.link);

I now get a different error:
Error: need 'this' for 'wl_container_of' of type 'pure nothrow 
@nogc @system item*(wl_list* ptr)'


I run dmd v2.067 on linux 64bits.

Any idea?

thanks
RĂ©mi




More information about the Digitalmars-d-learn mailing list