Templates convariance

pragma pragma_member at pathlink.com
Tue Jun 20 15:18:10 PDT 2006


In article <e799dr$2igf$1 at digitaldaemon.com>, michal.minich at gmail.com says...
>[snip]
>class List (T)
>{
>T item; // for simplicity, just one item possible in this list 
>}
>
>void renderShapes (List!(Shape) shapesList)
>{
>shapesList.item.render();
>}

Usually when I come across the need for something like that, I typically throw
in a common base class or interface for my template instances, so they are
castable from one to the other. 

interface IShapeList{ 
void renderShapes(IShapeList list);
}

class ShapeList(T): IShapeList{ /*...*/ }

As a side-effect, the above is now very friendly to use with non-template based
IShapeLists. ;)

I'm not a metaprogramming expert by any means, but I don't think covariance with
templates can be solved using a rule general enough to be implemented (easily)
in the compiler itself.  The problem is that templates allow one to create
interfaces derived from supplied types that can be composed in so many arbitrary
ways.

- EricAnderton at yahoo



More information about the Digitalmars-d mailing list