Thread overview
Template alias parameter: error: need 'this' for ...
Aug 23, 2013
Matej Nanut
Aug 23, 2013
Jonathan M Davis
Aug 24, 2013
Matej Nanut
August 23, 2013
Hello!

I've run into this issue that I don't understand, maybe someone can enlighten me. :)

This code:
---
struct Thing
{
    int i;
}

void main()
{
    t!(Thing.i)();
}

void t(alias a)()
{
    return;
}
---

fails to compile with: ‘Error: need 'this' for 't' of type 'pure nothrow @safe void()'’.

If I declare ‘t’ as static, it works (and does what I want it to do).
August 23, 2013
On Friday, August 23, 2013 23:28:46 Matej Nanut wrote:
> Hello!
> 
> I've run into this issue that I don't understand, maybe someone can enlighten me. :)
> 
> This code:
> ---
> struct Thing
> {
> int i;
> }
> 
> void main()
> {
> t!(Thing.i)();
> }
> 
> void t(alias a)()
> {
> return;
> }
> ---
> 
> fails to compile with: ‘Error: need 'this' for 't' of type 'pure
> nothrow @safe void()'’.
> 
> If I declare ‘t’ as static, it works (and does what I want it to
> do).

Because without static it's a member variable, which means that you have to have a constructed object to access it (since it's part of the object). When you declare a variable in a class or struct static, then there's only one for the entire class or struct, so it can be accessed without an object. And when you do StructName.var or ClassName.var your accessing the variable via the struct or class rather than an object, so the variable must be static.

- Jonathan M Davis
August 24, 2013
On Friday, 23 August 2013 at 22:54:33 UTC, Jonathan M Davis wrote:
> Because without static it's a member variable, which means that you have to
> have a constructed object to access it (since it's part of the object). When
> you declare a variable in a class or struct static, then there's only one for
> the entire class or struct, so it can be accessed without an object. And when
> you do StructName.var or ClassName.var your accessing the variable via the
> struct or class rather than an object, so the variable must be static.
>
> - Jonathan M Davis

But I declared the template static, not the variable.

Is there a better way to pass a ‘member get’ expression to a template?

I need this for calling ‘.offsetof’ on it, and for checking if the member's parent is a certain struct type.