Template alias parameter: error: need 'this' for ...

Jonathan M Davis jmdavisProg at gmx.com
Fri Aug 23 15:54:23 PDT 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


More information about the Digitalmars-d-learn mailing list