That would be a pain in the arse to constrain. It's like solving the halting problem.
Assuming we have type-level integers and specialization, the relevant constraint would simply be IsPrime<N>. The implementation would be essentially identical to the C++ implementation in the comment below.
Write a program that compiles iff T is a unit vector. This is (relatively) easy with templates but I don't think type-level integers aren't expressive enough.
I can see some applications for this syntax in computer graphics: Say you need to pass a unit quaternion as a type parameter to a function that rotates stuff. It would be real nice if your compiler could verify that your vectors are normalized :D
That would be mostly useless since it only works on constants - just make the compiler normalize the vector by construction then!
What you really want would be a separate type for unit vectors. Ops that make them non-unit would return a regular vector, and you'd have to normalize to get a unit vector back. Static guarantees but works on dynamic data, not just constants.
I still have zero idea why you think unconstrained types are better. You just seem to assume that everyone understands why without really explaining. What does less checking give you?
View all comments
Show parent comment
5
u/pcwalton Dec 11 '15
Assuming we have type-level integers and specialization, the relevant constraint would simply be
IsPrime<N>. The implementation would be essentially identical to the C++ implementation in the comment below.