Thread overview
Cross product template
May 15, 2013
Diggory
May 15, 2013
Diggory
May 15, 2013
Simen Kjaeraas
May 15, 2013
I have a vector struct, Vector(T, uint N) templated on the type T and number of components, N. I'm trying to write a function "cross" which will calculate the cross product of a number of vectors.

For a given number of components, N, the cross function should take N-1 arguments, each one a Vector!(?, N) and will return a vector perpendicular to the vectors passed in. The ? means the type is free to be anything.

The problem is that however I try to write it, the template argument deduction isn't powerful enough to work out which instantiation to use.

I thought something like this would work to deduce the parameters and then I could use constraints to enforce the other rules, but no:
auto cross(T, N, U...)(Vector!(T, N) a, U b) { return 0; }
May 15, 2013
On Wednesday, 15 May 2013 at 01:31:43 UTC, Diggory wrote:
> I have a vector struct, Vector(T, uint N) templated on the type T and number of components, N. I'm trying to write a function "cross" which will calculate the cross product of a number of vectors.
>
> For a given number of components, N, the cross function should take N-1 arguments, each one a Vector!(?, N) and will return a vector perpendicular to the vectors passed in. The ? means the type is free to be anything.
>
> The problem is that however I try to write it, the template argument deduction isn't powerful enough to work out which instantiation to use.
>
> I thought something like this would work to deduce the parameters and then I could use constraints to enforce the other rules, but no:
> auto cross(T, N, U...)(Vector!(T, N) a, U b) { return 0; }

Well, after much trial and error I ended up with this, which seems to work:

template VectorN(T : Vector!(T, N), uint N) {
	enum VectorN = N;
}

template isVectorN(uint N) {
	template isVectorN(T) {
		enum isVectorN = VectorN!(T) == N;
	}
}

auto cross(T...)(T args) if (allSatisfy!(isVectorN!(T.length+1), T) && T.length) {
	return 0;
}
May 15, 2013
On Wed, 15 May 2013 03:31:40 +0200, Diggory <diggsey@googlemail.com> wrote:

> I have a vector struct, Vector(T, uint N) templated on the type T and number of components, N. I'm trying to write a function "cross" which will calculate the cross product of a number of vectors.
>
> For a given number of components, N, the cross function should take N-1 arguments, each one a Vector!(?, N) and will return a vector perpendicular to the vectors passed in. The ? means the type is free to be anything.
>
> The problem is that however I try to write it, the template argument deduction isn't powerful enough to work out which instantiation to use.
>
> I thought something like this would work to deduce the parameters and then I could use constraints to enforce the other rules, but no:
> auto cross(T, N, U...)(Vector!(T, N) a, U b) { return 0; }

auto cross(T, uint N, U...)(Vector!(T,N) a, U b) { return 0; }

Oughta work.

-- 
Simen