[Issue 1661] Not possible to specialize on template with integer parameter

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Feb 20 19:31:22 PST 2008


http://d.puremagic.com/issues/show_bug.cgi?id=1661





------- Comment #3 from wbaxter at gmail.com  2008-02-20 21:31 -------
(In reply to comment #2)
> // This doesn't compile
> void test_specialize(T : Number!(int N), int N)() {
>     writefln("Ooh special - NUMBER");
> }
> 
> That correctly will not compile because Number!(int N) is invalid syntax.
> 

I think that's part of the problem -- there *is* no valid syntax for it.

For a template that takes a type you can specialize using (T:Container!(T)),
but a number is not a type so (T:Container!(T)) doesn't work (at least I'm
guessing that's why it doesn't work).

This doesn't look like it should work either because N hasn't been defined
anywhere.
void test_specialize(T : Number!(N))() {
    writefln("Ooh special - NUMBER N");
}

But it does compile. 
I tried the Number!(int N) version just because that looks like a reasonable
syntax for saying "hey we're introducing a new integer parameter named N here".


This affects code that uses templates to implement fixed-size vectors and
operations on them.  It came up when I was trying to implement Vector/Matrix
classes in OpenMesh/D:
http://www.dsource.org/projects/openmeshd/browser/trunk/OpenMeshD/OpenMesh/Core/Geometry/MatrixT.d
http://www.dsource.org/projects/openmeshd/browser/trunk/OpenMeshD/OpenMesh/Core/Geometry/VectorT.d

I don't remember exactly where I ran into it, but if you've got a Vector!(N)
it's pretty natural to want to write a template that matches only Vector! but
for any N.  If you're writing a template _function_ you can work around it by
putting the type as one of the function args.  But it's not always the case
(for instance if you want to make a Traits class specialized on Vector!(N).


-- 



More information about the Digitalmars-d-bugs mailing list