digitalmars.D.learn - Pass field as a template parameter
- Ben Jones (13/13) May 08 2019 I'm trying to write a template function like the below... is it
- Paul Backus (6/20) May 08 2019 void fun(string fieldName, alias p1, alias p2)()
I'm trying to write a template function like the below... is it possible without using string mixins? void fun( alias(?) field, alias p1, alias p2)() { if(p1.field) use(p2.field); } called with something like static foreach( list of fields){ fun!(field, p1, p2)(); } I have no idea how to specify the list of fields and I'm not sure if alias is the right "type" for the field template parameter.
May 08 2019
On Wednesday, 8 May 2019 at 18:01:53 UTC, Ben Jones wrote:I'm trying to write a template function like the below... is it possible without using string mixins? void fun( alias(?) field, alias p1, alias p2)() { if(p1.field) use(p2.field); } called with something like static foreach( list of fields){ fun!(field, p1, p2)(); } I have no idea how to specify the list of fields and I'm not sure if alias is the right "type" for the field template parameter.void fun(string fieldName, alias p1, alias p2)() { if (__traits(getMember, p1, fieldName)) use(__traits(getMember, p2, fieldName)); }
May 08 2019