On Wednesday, 29 July 2015 at 21:33:16 UTC, remi thebault wrote:
Hello

I have this weird error trying to achieve something simple:

That's far from simple.

Here's a reduction:

----
template wl_container_of(alias member)
{
    size_t get() {return member.offsetof;}
}

struct item { int link; }

void main()
{
wl_container_of!(item.link).get(); /* Error: 'this' is only defined in non-static member functions, not main */
}
----

I'm not sure what's going on here, if this should or shouldn't work. The error message isn't exactly good.

Slapping `static` on `get` seems to make it work:
----
    static size_t get() {return member.offsetof;}
----

I guess the compiler thinks that since `item.link` is an instance member, it needs a `this`. And then `get` would need a `this` too as it's using `item.link` via `member`. When the compiler sees that there is no `this`, it errors out.

An explicit `static` forces the compiler to assume no `this`. And it would error out if you actually tried to make use of it.

Naively, I'd think the compiler should be able to figure that out itself.

Reply via email to