April 01, 2005
How do I inherit a template class?

I have code like this:

// Define templated Vector class:
class Vector(alias type, int size) {
..
}

// Inherit the Vector class to create a Point3D class:
class Point3D : Vector!(float, 3) {
..
}

The compiler reports on the definition of class Point3D line with this error: "template instance Vector!(float,3) does not match any template declaration"

If I try to inherit Vector with no template arguments, the compiler reports on
the same line with this error:
"template dts_point.Vector(alias type,int size) is used as a type"


It seems that D's template system is either missing some of C++'s template features, or is not documenting how to achieve equal functionality.

Regards,
James Dunne
April 02, 2005
Your declaration

> class Vector(alias type, int size) {

Should be written as
    class Vector(type, int size) ....

Please read
http://www.digitalmars.com/d/template.html

for the meaning of word 'alias' in template declarations.

In short 'alias'ed paramater in D template is not a name of a class
(typename) but
alias of some object.  And you are right - there is no direct C++ equivalent
for that.

Take a look on Ben's examples: http://home.comcast.net/~benhinkle/mintl/

Andrew.
http://terrainformatica.com



"James Dunne" <jdunne4@bradley.edu> wrote in message news:d2kmrt$1rnp$1@digitaldaemon.com...
> How do I inherit a template class?
>
> I have code like this:
>
> // Define templated Vector class:
> class Vector(alias type, int size) {
> ..
> }
>
> // Inherit the Vector class to create a Point3D class:
> class Point3D : Vector!(float, 3) {
> ..
> }
>
> The compiler reports on the definition of class Point3D line with this
> error:
> "template instance Vector!(float,3) does not match any template
> declaration"
>
> If I try to inherit Vector with no template arguments, the compiler
> reports on
> the same line with this error:
> "template dts_point.Vector(alias type,int size) is used as a type"
>
>
> It seems that D's template system is either missing some of C++'s template features, or is not documenting how to achieve equal functionality.
>
> Regards,
> James Dunne