Rebind template(bug?)

Jack Applegame via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 22 00:54:36 PDT 2016


On Monday, 22 August 2016 at 00:43:00 UTC, Engine Machine wrote:
> The following code works and does what I want!
>
> template InstantiateOrEmptySeq(alias tmpl, args...)
> {
>     alias Seq(T...)=T;
>     static if (args.length > 0)
>         alias InstantiateOrEmptySeq = tmpl!(args[0 .. $-1]);
>     else
>         alias InstantiateOrEmptySeq = Seq!();
> }
>
>
> class T(A...) : InstantiateOrEmptySeq!(T, A)
> {	
> 	static if (A.length == 0)
> 	{
>                 // Base class
> 		int x;
> 	} else	
> 	static if (A[$-1] == "Animal")
> 	{
> 		int y;
> 	} else
> 	static if (A[$-1] == "Dog")
> 	{
> 		int z;
> 	} else
> 	static if (A[$-1] == "Pug")
> 	{
> 		int s;
> 	} else static assert(A[$-1]~" not a defined class of 
> "~this.stringof);
> }

Why don't you like a cleaner (in my opinion) solution?

class T() {
	// Base class
	int x;
}

class T(A...) : T!(A[0..$-1]) {	
	static if (A[$-1] == "Animal")
	{
		int y;
	} else
	static if (A[$-1] == "Dog")
	{
		int z;
	} else
	static if (A[$-1] == "Pug")
	{
		int s;
	} else static assert(A[$-1]~" not a defined class of 
"~this.stringof);
}


More information about the Digitalmars-d-learn mailing list