Topic: Templated classes, inheritance, and global templated functions


Author: Per Angstrom <eri.edt.edtpang@memo.ericsson.se>
Date: 1995/06/09
Raw View
bglenden@colobus.cv.nrao.edu (Brian Glendenning) wrote:
>
>Is the C++ standard going to guarantee that code like the following
>will work:
>
>template<class T> struct X
>{ ... };
>
>template<class T> struct Y : public X<T>
>{ ... };
>
>template<class T> void takes_base_class_reference(const X<T> &x) // template fn
>{ ... }
>
>int main()
>{
>    Y<int> y1;
>    takes_base_class_reference(y1);
>    ...
>}
>
>I thought that while the ARM disallowed this conversion (derived reference to
>base reference) in a templated global function, that it was certain to be in
>the forthcoming standard. However, I have recently had a knowledgable source
>tell me that this might not happen.
>
>IMO, if conversions like this are not allowed, it will be hard to meaningfully
>use polymorphic templated classes.
>
I ran into a similar problem the other day, but I thought it was due to a
compiler bug. I got round it by using an explicit typecast in the invocation
of the function, but that is hardly an acceptable workaround, IMHO.
If your source is correct, which I hope not, there had better be some very
strong reasons for disallowing this construct in
the Standard.

Per Angstrom (eri.edt.edtpang@memo.ericsson.se)






Author: bglenden@colobus.cv.nrao.edu (Brian Glendenning)
Date: 1995/06/09
Raw View
Is the C++ standard going to guarantee that code like the following
will work:

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

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

template<class T> void takes_base_class_reference(const X<T> &x) // template fn
{ ... }

int main()
{
    Y<int> y1;
    takes_base_class_reference(y1);
    ...
}

I thought that while the ARM disallowed this conversion (derived reference to
base reference) in a templated global function, that it was certain to be in
the forthcoming standard. However, I have recently had a knowledgable source
tell me that this might not happen.

IMO, if conversions like this are not allowed, it will be hard to meaningfully
use polymorphic templated classes.

Thanks for your insights.

Brian

--
       Brian Glendenning - National Radio Astronomy Observatory
bglenden@nrao.edu          Charlottesville Va.           (804) 296-0286