Is there a template syntax for alias-of-member?

Russell Lewis webmaster at villagersonline.com
Fri Mar 7 07:26:27 PST 2008


Here's what I'm trying to do.  I want to build a template which takes, 
as its first parameter, a type, and then takes one or more aliases to 
member names.  In other words, the user of the template would write 
something like this:

BEGIN CODE (that doesn't work)
	struct s
	{
		int x;
	}
	void main()
	{
		magic_template!(s, x);
	}
END CODE

However, I can't figure out how to do it.  Right now, I'm stumbling 
along with string mixins, as follows:

BEGIN CODE
	template foo(T,string member)
	{
	  T *of()
	  {
	    T *ret = new T;
	    mixin("ret." ~member~ "= 1;");
	    return ret;
	  }
	}

	struct s
	{
	  int x;
	}

	import std.stdio;
	void main()
	{
	  auto tmp = foo!(s,"x").of();
	  writefln(tmp.x);
	}
END CODE


Does anybody know of a more elegant way to do this?


More information about the Digitalmars-d-learn mailing list