Topic: Is it legal to derive from a template parameter?


Author: siemelnaran@my-deja.com
Date: 2000/12/05
Raw View
See subject for question: Is it legal to derive from a template
parameter?

I think I answered this question for somebody once (and the answer was
no if I remember correctly), but I just can't remember anymore.  If it
is illegal, what is the substitute and is a bug fix in order?

--
---------------------
Siemel Naran


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: "Richard Parkin" <rparkin@nospam.msi-eu.com>
Date: 2000/12/05
Raw View
<siemelnaran@my-deja.com> wrote in message
news:90hrl4$9ph$1@nnrp1.deja.com...
> See subject for question: Is it legal to derive from a template
> parameter?
>
> I think I answered this question for somebody once (and the answer was
> no if I remember correctly), but I just can't remember anymore.  If it
> is illegal, what is the substitute and is a bug fix in order?

Do you mean

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

then afaik, the answer is yes. I'm just revisiting a paper from the template
workshop at Erfurt which discusses this 'mixin' idiom and a solution to one
of it's problems. http://www.oonumerics.org/tmpw00/eisenecker.html

14.1/13 implies that the name is in scope and usuable at that point, so it's
known about.
14.6.1/3 says something similar and has the example
template < class T > class X : public Array<T> { /*...*/ };
14.6.2 has more of the same.

Ah, bingo!
14.6.2/4 - "If a base type is a dependent type"...and example
template < class T > struct Y : T { ... };

I'm convinced.

HTH

Ric


---
[ 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://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: James Kuyper <kuyper@wizard.net>
Date: 2000/12/05
Raw View
siemelnaran@my-deja.com wrote:
>
> See subject for question: Is it legal to derive from a template
> parameter?
>
> I think I answered this question for somebody once (and the answer was
> no if I remember correctly), but I just can't remember anymore.  If it
> is illegal, what is the substitute and is a bug fix in order?

There are a couple of different things you might be referring to, which
as far as I can tell are legal:

template<class T> struct A : public T // Case 1: Legal
{
 struct B : public T { // Case 2: Legal
 } b;
};

A<int> a; // Illegal - can't derive from non-class type

struct C { };

A<C> ac; // Legal

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]