Topic: template overloading


Author: bs@alice.att.com (Bjarne Stroustrup)
Date: 11 Sep 92 18:44:03 GMT
Raw View

girod@tele.nokia.fi (Marc Girod @ kpd) writes

 > This made me wonder whether it was possible to overload template
 > definitions, such as:
 >
 > template <class A> class X {};
 > template <class A, class B> class X {};
 >
 > X<int> x1;       //from the first template
 > X<int, int> x2;  //from the second
 >
 > I checked with g++ v2.2.2, and was surprised to notice that the
 > overriding of the templates was legal, but thus hid the former
 > declaration, without any warning. The result was thus an error, at
 > delaration time of x1:
 >
 > incorrect number of parameters (1, should be 2) in template expansion
 >
 > Well, I had a look in ARM, and couldn't find anything better than:
 >
 > p 347
 > 14.5c Declarations and Definitions
 > "There must be exactly one definition for each template of a given
 > name in a program."


How about (pg343):

 A class template name must be unique in a program and may not
 be declared to refer to any other template, class, function,
 value, or type in the same scope.

 > This sounds like g++ was right (apart from allowing the superseeding
 > to occur, additionally without warning).

No. That seems to be a g++ bug.

 > But now, why? What would be the problem? Wouldn't it be more natural
 > to allow template name overloading?

It would be nice to be able to overload templates, but to do this we'd
have to work out the exact overloading rules. That is not a trivial task,
it hasn't been done, and there may be fundamental problems lurking.
Hence the prohibition.




Author: girod@tele.nokia.fi (Marc Girod)
Date: Fri, 11 Sep 1992 11:15:10 GMT
Raw View
Someone proposed some time ago that a void type would be created,
which would be guarrantied to be of null length, which eventually gave
birth to various threads.

The originally alleged use for such a type would have been a special
use of certain templates, as follows:

template <class A, class B> class X;

X<int, void> x;

This made me wonder whether it was possible to overload template
definitions, such as:

template <class A> class X {};
template <class A, class B> class X {};

X<int> x1;       //from the first template
X<int, int> x2;  //from the second

I checked with g++ v2.2.2, and was surprised to notice that the
overriding of the templates was legal, but thus hid the former
declaration, without any warning. The result was thus an error, at
delaration time of x1:

incorrect number of parameters (1, should be 2) in template expansion

Well, I had a look in ARM, and couldn't find anything better than:

p 347
14.5c Declarations and Definitions
"There must be exactly one definition for each template of a given
name in a program."

This sounds like g++ was right (apart from allowing the superseeding
to occur, additionally without warning).

But now, why? What would be the problem? Wouldn't it be more natural
to allow template name overloading?
--
+-----------------------------------------------------------------------------+
| Marc Girod - Nokia Telecommunications       Phone: +358-0-511 7703          |
| TL4E - P.O. Box 12                            Fax: +358-0-511 7432          |
| SF-02611 Espoo 61 - Finland              Internet: marc.girod@ntc.nokia.com |
|    X.400: C=FI, A=Elisa, P=Nokia Telecom, UNIT=TRS, SUR=Girod, GIV=Marc     |
+-----------------------------------------------------------------------------+