Topic: Inheritance and Templates
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Sun, 26 Jun 1994 09:00:03 GMT Raw View
cmc@ecrc.de (Chris Crampton) writes:
>I wonder if anyone can clarify the following problem: do the "normal"
>rules of type conversion between related classes hold even when
>templates are involved?
No. There are a different set of rules for templates.
>As class B is derived from class A, can't references to objects of type
>B be also used where references to objects of type A are required?
>Shouldn't the compiler perform the required type-conversion?
According to the ARM, no. But the committee has decided to allow your
code, and it is legal according to the latest draft of the working
paper.
>Both GNU G++ (2.5.7) and Sun C++ 4.0 will not accept the above code
>while ObjectCenter (based on cfront 3.0) finds the code okay. Sun
>engineers claim that the ARM (p.346) supports their view but I cannot
>find anything there that backs them up.
The Sun engineers are correct - ARM p. 346 states that "Not even
trivial conversions will be applied" in the case of calls to template
functions. However as noted above, the latest working draft of the
working paper relaxes this somewhat, allowing various conversions
including the one in your example code.
--
Fergus Henderson - fjh@munta.cs.mu.oz.au
Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Mon, 27 Jun 1994 16:17:14 GMT Raw View
In article <CrHGDq.93G@ecrc.de> cmc@ecrc.de writes:
>
>I wonder if anyone can clarify the following problem: do the "normal"
>rules of type conversion between related classes hold even when
>templates are involved?
Yes, but the "normal" rules for overload resolution do not.
[BTW: Please write code example so they fit in the least
number of lines without being overly long. Some of us dont
have nice X-terminals :-[
>template<class T> class A {};
>class B : public A<char> {};
>template<class T> void foo (A<T>& a) {}
>
>main () { B b; foo (b); return 0; }
>--
>
>As class B is derived from class A, can't references to objects of type
>B be also used where references to objects of type A are required?
>Shouldn't the compiler perform the required type-conversion?
Derived to Base conversions were allowed for templates
at San Diego.
>
>Both GNU G++ (2.5.7) and Sun C++ 4.0 will not accept the above code
>while ObjectCenter (based on cfront 3.0) finds the code okay. Sun
>engineers claim that the ARM (p.346) supports their view but I cannot
>find anything there that backs them up.
They are right. ARM does not allow ANY conversions matching
function templates. Not even trivial ones (which dont exist
anymore :-)
--
JOHN (MAX) SKALLER, INTERNET:maxtal@suphys.physics.su.oz.au
Maxtal Pty Ltd, CSERVE:10236.1703
6 MacKay St ASHFIELD, Mem: SA IT/9/22,SC22/WG21
NSW 2131, AUSTRALIA
Author: cmc@ecrc.de (Chris Crampton)
Date: Thu, 16 Jun 1994 09:15:25 GMT Raw View
I wonder if anyone can clarify the following problem: do the "normal"
rules of type conversion between related classes hold even when
templates are involved?
For example, take the following code:
--
template<class T>
class A
{};
class B : public A<char>
{};
template<class T>
void foo (A<T>& a)
{}
main ()
{
B b;
foo (b);
return 0;
}
--
As class B is derived from class A, can't references to objects of type
B be also used where references to objects of type A are required?
Shouldn't the compiler perform the required type-conversion?
Both GNU G++ (2.5.7) and Sun C++ 4.0 will not accept the above code
while ObjectCenter (based on cfront 3.0) finds the code okay. Sun
engineers claim that the ARM (p.346) supports their view but I cannot
find anything there that backs them up.
If the above is indeed illegal, I find this worrying as it would seem
to contravene the rules of inheritance, one of the key features of OOP
and C++.
Cheers,
Chris.
---
Chris Crampton Email: cmc@ecrc.de
European Computer-industry Research Centre GmbH
Arabellastrasse 17 Phone: +49 89 92699 138
D-81925 Munich FAX: +49 89 92699 170
Germany
Author: tedlaw@vnet.ibm.com ()
Date: Thu, 16 Jun 1994 13:47:24 GMT Raw View
Just a data point... The IBM CSet++ compiler accepts your code
without complaint.
Ted Law
(tedlaw@vnet.ibm.com)