Is there a way to set an alias to specific form of a template?

Roderick Gibson kniteli at gmail.com
Fri Oct 7 23:43:09 PDT 2011


On 10/7/2011 7:33 PM, Andrej Mitrovic wrote:
> You don't have to rewrite Vector for multiple dimensions, methinks:
>
> class Vector(T...) {
>      this(T t) {}
> }
>
> void main()
> {
>      alias Vector!(float, float) vec2f;
>      auto v = new vec2f(1.0,1.0);
> }
>
> You'll probably have to play with `static if`, template constraints,
> and stuff like that.

I got this working, found an odd bug though. This code,

alias Vector!(float, float) vec2f;
alias Vector!(double, double) vec2d;
alias Vector!(float, float, float) vec3f;

public struct Vector(T...) {
	int dim = 0;

	this(T...)(T args) {
		dim = args.length;
	}

	unittest {
		auto v = new vec2f(1.2f, 1.5f);
		vec2f d = new vec2f(1.1f, 1.4f);

		assert(v.dim == 2);
		assert(d.dim == 2);
	}
}

will pass the first assert and fail on the second. Checking out the 
contents of v.length and d.length with writeln gives the correct answer 
on the first and
2
1
RANDOMHEXCODE
on the second. Very strange.


More information about the Digitalmars-d-learn mailing list