digitalmars.D.learn - Template alias parameter: error: need 'this' for ...
- Matej Nanut (22/22) Aug 23 2013 Hello!
- Jonathan M Davis (8/36) Aug 23 2013 Because without static it's a member variable, which means that you have...
- Matej Nanut (6/19) Aug 24 2013 But I declared the template static, not the variable.
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).
Aug 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
Aug 23 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 DavisBut 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.
Aug 24 2013