Topic: Templates and Friends
Author: ldup@ta7s49.alcbel.be (Luc Duponcheel)
Date: 28 Jan 93 12:54:57 GMT Raw View
I have some question about templates and friends.
Since friend function templates are just like function templates,
they cannot have non-type template arguments.
Therefore the following example will not compile
---------begin------------
#include <iostream.h>
template<class T, int size>
class BoundedContainer {
friend ostream &operator<<(ostream &, const BoundedContainer<T,size> &) ;
// more stuff
} ;
template<class T, int size>
ostream &operator<<(ostream &os, const BoundedContainer<T,size> &bC) {
// do something
return os ;
}
----------end-------------
my questions are :
------------------
1) What is the very reason for not allowing non-type template arguments
for function (and therefore also friend function) templates?
2) Is there a way to solve my problem?
Luc Duponcheel
Alcatel Bell Telephone, SE99
Francis Wellesplein 1,
B-2018 Antwerpen
Belgium
ldup@alcbel.be
\_ \_\_\_ \_ \_ \_\_\_
\_ \_ \_ \_ \_ \_ \_
\_ \_ \_ \_ \_ \_\_\_ __o
\_ \_ \_ \_ \_ \_ _`\<,_
\_\_\_\_ \_\_\_ \_\_\_\_ \_ (*)/ (*)
Author: pjl@cs.uiuc.edu (Paul Lucas)
Date: Fri, 29 Jan 1993 22:37:25 GMT Raw View
In <1956@alcbel.be> ldup@ta7s49.alcbel.be (Luc Duponcheel) writes:
>I have some question about templates and friends.
>Since friend function templates are just like function templates,
>they cannot have non-type template arguments.
>Therefore the following example will not compile
>---------begin------------
>#include <iostream.h>
>template<class T, int size>
>class BoundedContainer {
>friend ostream &operator<<(ostream &, const BoundedContainer<T,size> &) ;
>// more stuff
>} ;
>template<class T, int size>
>ostream &operator<<(ostream &os, const BoundedContainer<T,size> &bC) {
> // do something
> return os ;
>}
>----------end-------------
>my questions are :
>------------------
>1) What is the very reason for not allowing non-type template arguments
>for function (and therefore also friend function) templates?
Functions can only be overloaded by (differing) type, not
differing values of the same type (I think).
>2) Is there a way to solve my problem?
Your code compiles just fine under cfront.
--
- Paul J. Lucas
AT&T Bell Laboratories
Naperville, IL
Author: jamshid@emx.cc.utexas.edu (Jamshid Afshar)
Date: 29 Jan 1993 18:36:47 -0600 Raw View
In article <C1n06E.Hqy@cs.uiuc.edu> pjl@cs.uiuc.edu (Paul Lucas) writes:
>In <1956@alcbel.be> ldup@ta7s49.alcbel.be (Luc Duponcheel) writes:
>>template<class T, int size>
>>ostream &operator<<(ostream &os, const BoundedContainer<T,size> &bC) {
>> // do something
>> return os ;
>>}
>
>>1) What is the very reason for not allowing non-type template arguments
>>for function (and therefore also friend function) templates?
>
> Functions can only be overloaded by (differing) type, not
> differing values of the same type (I think).
Huh? ARM 14.4c says "All template-args for a function template must
be type-arguments." The commentary gives the reason "[template
arguments] must be deduced from the actual arguments in calls of the
template function." I don't understand this reason, but it seems
clear the ARM does not allow function templates to have non-type
arguments.
Lippman's _C++ Primer 2nd Ed_ (p200, end of 4.2) says permitting
template functions to accept expression parameters is a likely
proposal to the ANSI C++ committee. Has such a proposal been made,
and if so, would anybody care to guess what are the chances of it
passing?
>>2) Is there a way to solve my problem?
> Your code compiles just fine under cfront.
So cfront implements this extension? Besides the situation above, I
think non-type template arguments would be of (dubious?) use for
built-in arrays. Here's one solution to the recently-discussed
problem of initializing an Array class object with a set of values.
I'm still not sure how I feel about it stylisticly, but it is safer
than the other solutions mentioned.
template<class T> class Array {
public:
Array(size_t n, const T* init);
//...
};
template<class T, size_t SZ> inline // currently illegal
Array<T> to_array(T (const &a)[SZ])
{ return Array<T>(SZ,a); }
//...
static double tbl[] = { 1.1, 2.2, 3.3, 4.4 };
Array<T> table = to_array(tbl); // compilers should optimize
Of course if nested templates were allowed, you could have the (rather
nice, IMHO) solution of:
template<class T> class Array {
public:
template<size_t SZ>
Array(T (const &a)[SZ]);
//...
};
static double tbl[] = { 1.1, 2.2, 3.3, 4.4 };
Array<T> table = tbl;
Has a proposal for nested templates been made? Fortunately I don't
think I feel strongly enough about it to make a proposal, but I am
curious.
Jamshid Afshar
jamshid@emx.utexas.edu
Author: mat@mole-end.matawan.nj.us
Date: Thu, 4 Feb 1993 17:23:39 GMT Raw View
In article <1kciivINN6v9@emx.cc.utexas.edu>, jamshid@emx.cc.utexas.edu (Jamshid Afshar) writes:
> In article <C1n06E.Hqy@cs.uiuc.edu> pjl@cs.uiuc.edu (Paul Lucas) writes:
> Lippman's _C++ Primer 2nd Ed_ (p200, end of 4.2) says permitting
> template functions to accept expression parameters is a likely
> proposal to the ANSI C++ committee. Has such a proposal been made,
> and if so, would anybody care to guess what are the chances of it
> passing?
The proposal in question would require that all the template arguments of
a function template _be involved in determining_ the types of the template
function's arguments. Thus, this would be allowed:
template < class A, len >
A sum( Array< A, len > )
.. .. ..
but this would not:
template < class A, len >
A flake( Array< A, 1 >, len )
.. .. ..
because there is no way, given a call of flake() , to determine a value
for len .
And yes, I believe it's going in.
--
(This man's opinions are his own.)
From mole-end Mark Terribile
mat@mole-end.matawan.nj.us, Somewhere in Matawan, NJ
Author: mat@mole-end.matawan.nj.us
Date: Fri, 5 Feb 1993 16:53:38 GMT Raw View
Scott Meyers has been kind enough to point out that I bumbled the code
examples below:
Good:
> template < class A, len >
> A sum( Array< A, len > )
> .. .. ..
Bad:
> template < class A, len >
> A flake( Array< A, 1 >, len )
> .. .. ..
Of course the `len' should be `int len' in three of the places where it
appears.
--
(This man's opinions are his own.)
From mole-end Mark Terribile
mat@mole-end.matawan.nj.us, Somewhere in Matawan, NJ