Topic: templates in an inheritance hierarchy


Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 2000/03/24
Raw View
Greg Comeau wrote:
>
> In article <38CE6F61.1475F5BA@physik.tu-muenchen.de> Christopher Eltschka <celtschk@physik.tu-muenchen.de> writes:
> >Well, there's no template class, but only a class template.
> >That may sound like nit-picking, but the difference is important
> >- you immediatly see that deriving a class from a template makes
> >no sense: a class is always derived from a class.
> >Now, a class template is a means for generating classes. Of
> >course you can derive a class from another class which was
> >generated through a template. The name of that class is given
> >by the template name, followed by the template parameters.
>
> The class produced from a `class template' is normally spoken of
> as a `template class'.

I wasn't aware of that. Indeed, I cannot remember having
read this term in a context where is wasn't used instead of
"class template" (of course, it may just be my memory).

IMHO there is generally no need to give the instance of
a class template a special name, since there's nothing special
about it, except that you write it with those <...> at the end.

I've now tried a Deja search on "template class" in
comp.lang.c++.moderated, and looked on the first 25 hits.
Of the real hits (the search of course gave everything
containing "template" and "class") were a lot of those,
which use "template class" where "class template" was
meant, including two who obviously didn't make the proper
distinction at all (i.e. confused the template and the
generated class); two postings used the term "template class"
in a way which allow both interpretations, and only one uses
it in a way which suggests your definition.

Therefore I conclude, even if the definition of
"template class" as "class produced from class template"
really _is_ used sometimes, it's a term which leads
to confusion and should therefore not be used. Especially
since I don't see a single case where a class produced
from a class template behaves differently than a normal
class.
There _are_ special rules for the class templates themselves,
like different lookup rules. However the classes produced
from those templates don't differ (esp. AFAIK the normal
lookup rules apply for them).

> Saying it is not the case is not only not (phew)
> nitpicking but has little basis, especially when wanting to sometimes
> talk about `non-template classes'.

How often do you want to talk about "non-template classes"?
And why?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Olinga K. Abbott" <olinga@usa.net>
Date: 2000/03/14
Raw View
Templates have been presenting me with a problem when used in combination
with inheritance. Specifically, I need to know how to inherit: 1) a
non-template class from a template class; 2) a template class from a
non-template class; and 3) inherit a template class from a template class. I
would like to see specific syntax, not just a general description.


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Alan Donovan <adonovan@imerge.co.uk>
Date: 2000/03/14
Raw View
"Olinga K. Abbott" wrote:
>
> Templates have been presenting me with a problem when used in combination
> with inheritance. Specifically, I need to know how to inherit: 1) a
> non-template class from a template class; 2) a template class from a
> non-template class; and 3) inherit a template class from a template class. I
> would like to see specific syntax, not just a general description.


// template class
template <class T>
struct C
{
    int a;
    T   m;
};

// non-template derived from template
struct D : C<char *>
{
};

// template derived from non-template
template <class T>
struct E : D
{
    T k;
};

// template derived from template
template <class T>
struct F : C<T*>
{
 // note that type expressions are allowed, e.g.
 // C<T*> instead of just C<T>.
};

int main()
{
    C<int> g;
    g.a = 0; g.m = 1;

    D h;
    h.a = 2; h.m = "hello";

    E<int *> i;
    i.k = &g.a; i.a = 3; i.m = "world";

    F<int> j;
    j.m = new int; j.a = 4;
}

------------------------------------------------------------------------
  Alan Donovan     adonovan@imerge.co.uk    http://www.imerge.co.uk
  Imerge Ltd.      +44 1223 875265

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: comeau@panix.com (Greg Comeau)
Date: 2000/03/14
Raw View
In article <sco9lgrua5n31@corp.supernews.com> "Olinga K. Abbott" <olinga@usa.net> writes:
>Templates have been presenting me with a problem when used in combination
>with inheritance. Specifically, I need to know how to inherit: 1) a
>non-template class from a template class;

template <typename T> class b { };
class d : public b<int> { }

>2) a template class from a non-template class;

template <typename T> class e : public d { };

>and 3) inherit a template class from a template class. I
>would like to see specific syntax, not just a general description.

template <typename T> class f : public b<int> { };
template <typename T> class g : public b<T> { };

- Greg
--
Comeau Computing, Producers of Comeau C/C++ 4.2.42 (4.2.43 BETA starting)
Try Comeau C++ online at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "TiTi" <cenTipod@anTi.com>
Date: 2000/03/14
Raw View
> Templates have been presenting me with a problem when used in combination
> with inheritance. Specifically, I need to know how to inherit: 1) a
> non-template class from a template class; 2) a template class from a
> non-template class; and 3) inherit a template class from a template class.
I
> would like to see specific syntax, not just a general description.




template <class T>
class X {};

class Y{};


1) class NonTemplateFromTemplate : public X<int> {};
2) template <class Z> class TemplateFromNonTemplate : public Y {};
3a) template <class Z> class TemplateFromTemplate : public X<Z> {};
3b) template <class W, class Z> class TemplateFromTemplate : public X<Z> {};



TiTi



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 2000/03/15
Raw View
"Olinga K. Abbott" wrote:
>
> Templates have been presenting me with a problem when used in combination
> with inheritance. Specifically, I need to know how to inherit: 1) a
> non-template class from a template class;

Well, there's no template class, but only a class template.
That may sound like nit-picking, but the difference is important
- you immediatly see that deriving a class from a template makes
no sense: a class is always derived from a class.
Now, a class template is a means for generating classes. Of
course you can derive a class from another class which was
generated through a template. The name of that class is given
by the template name, followed by the template parameters.
Since this is just a normal class, you can use it in any way
as a normal class, including deriving from it, with exactly
the same syntax.

That is, if you have

template<typename T> class X { ... };

then you have a class template X, from which you can instantiate
(= generate) classes X<int>, X<double> or even X< X<int> >.
To derive from such a class, you just use it like any other class:

class Y:
  public X<A> // Derived from class X<A>
{
  ...
};

A very interesting special case is if you instantiate
the template on the class you are currently defining:

class Z:
  public X<Z>
{
  ...
};

> 2) a template class from a
> non-template class;

Again, if you consider that it's a class template, the only
reasonable interpretation is: "a template for a class which is
derived from another class".

Therefore you write:

template<typename T> // we want a template parametrized on a type
                     // which we call T
 class A:            // The classes shall be named A<...>
  public Y           // and shall be derived from class Y
{
  ...
};

You also can derive from the template parameter:

template<typename T> class B:
  public T // The class shall be derived from the parameter
           // used to instantiate the template (of course, that
           // must be a class in this case)
{
  ...
};

> and 3) inherit a template class from a template class. I
> would like to see specific syntax, not just a general description.

Not this is a template for a class which is derived from
another class which was instantiated from another template.

template<typename T> class C:
  public X<Y>
{
  ...
};

Of course, the base class can (and most often will) depend on the
template parameter given:

template<typename T> class D:
  public C<T>
{
  ...
};

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: comeau@panix.com (Greg Comeau)
Date: 2000/03/15
Raw View
In article <38CE6F61.1475F5BA@physik.tu-muenchen.de> Christopher Eltschka <celtschk@physik.tu-muenchen.de> writes:
>Well, there's no template class, but only a class template.
>That may sound like nit-picking, but the difference is important
>- you immediatly see that deriving a class from a template makes
>no sense: a class is always derived from a class.
>Now, a class template is a means for generating classes. Of
>course you can derive a class from another class which was
>generated through a template. The name of that class is given
>by the template name, followed by the template parameters.

The class produced from a `class template' is normally spoken of
as a `template class'.  Saying it is not the case is not only not (phew)
nitpicking but has little basis, especially when wanting to sometimes
talk about `non-template classes'.

- Greg
--
Comeau Computing, Producers of Comeau C/C++ 4.2.42 (4.2.43 BETA starting)
Try Comeau C++ online at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]