Topic: Can template function be friend of non-temp


Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Wed, 28 Dec 1994 03:13:24 GMT
Raw View
girod@dshp01.trs.ntc.nokia.com (Marc Girod) writes:

>>>>>> "Steve" == Steve Clamage <clamage@Eng.Sun.COM> writes:
>
>Steve> The language rule was always (and certainly in 1992) that
>Steve> templates could be declared only at file scope.
>
>I think there may have been a confusion...
>Declaration of a family of templates as friend was possible earlier
>already,

No, that is NOT correct.

At least one compiler (Cfront) allowed it, and at least one textbook
(Lipmann) mentioned it, but the definitive references - the ARM and the
ANSI/ISO committee's working draft - have never allowed it, and neither
have many (most?) other compilers.  It is still the case that template
friend declarations are not allowed according to the latest ANSI/ISO WD.

--
Fergus Henderson - fjh@munta.cs.mu.oz.au




Author: "Ronald F. Guilmette" <rfg@rahul.net>
Date: 22 Dec 1994 19:40:10 GMT
Raw View
In article <3d5jv3$qfc@engnews2.Eng.Sun.COM>,
Steve Clamage <clamage@Eng.Sun.COM> wrote:
>girod@dshp01.trs.ntc.nokia.com (Marc Girod) writes:
>
>
>>I think there may have been a confusion...
>>Declaration of a family of templates as friend was possible earlier
>>already, with a slightly different syntax though (the placement of the
>>template keyword). It is simpler if the prototype was seen already. In
>>any case the template is not declared in the class scope through the
>>friend declaration, but in the ...(don't remember and was not trivial :-)
>
>You'll have to be more explicit than that. Lippman's book contains
>an example of declaring a set of templates as friends of a class,
>but I never heard of a compiler which would compile that code. It
>contradicted the stated rules for templates at that time. If in fact
>there was a way to do it, I'd like to know about it, and about which
>compilers supported the method.
>--
>Steve Clamage, stephen.clamage@eng.sun.com

We really should provide the precise reference... for those that are
interested enough to look it up.

The example in question is the middle one on page 361 of Lippman's 2nd
edition.

--

-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- E-mail: rfg@segfault.us.com ----------- Purveyors of Compiler Test ----
-------------------------------------------- Suites and Bullet-Proof Shoes -




Author: imy@wcl-rs.bham.ac.uk (Ian Young)
Date: 19 Dec 1994 11:50:17 GMT
Raw View
In article <GIROD.94Dec19123254@dshp01.trs.ntc.nokia.com>,
Marc Girod <girod@trshp.trs.ntc.nokia.com> wrote:
>Declaration of a family of templates as friend was possible earlier
>already, with a slightly different syntax though (the placement of the
>template keyword).

[ template <class T> friend void foo(T*); // rather than
  friend template <class T> void foo(T*); // (as I had it)]

Sadly, both my xlC and g++ 2.5.8 reject your example with either
positioning of the friend keyword. I think I need to get hold of a
newer compiler :-)

>class Z;
>template <class T> class Y;
>template <class T> void foo(T*);
>
>class X {
>  friend class Z;
>  template <class T> friend class Y;
>  template <class T> friend void foo(T*);
>};

>In any case the template is not declared in the class scope through
>the friend declaration, but in the ...(don't remember and was not
>trivial :-)

"If a class or a function mentioned as a friend has not been declared
 its name is entered in the same scope as the name of the class
 containing the friend declaration." (ARM    9.1 &    11.4)

Cheers,

Ian.




Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 20 Dec 1994 03:44:35 GMT
Raw View
girod@dshp01.trs.ntc.nokia.com (Marc Girod) writes:


>I think there may have been a confusion...
>Declaration of a family of templates as friend was possible earlier
>already, with a slightly different syntax though (the placement of the
>template keyword). It is simpler if the prototype was seen already. In
>any case the template is not declared in the class scope through the
>friend declaration, but in the ...(don't remember and was not trivial :-)

You'll have to be more explicit than that. Lippman's book contains
an example of declaring a set of templates as friends of a class,
but I never heard of a compiler which would compile that code. It
contradicted the stated rules for templates at that time. If in fact
there was a way to do it, I'd like to know about it, and about which
compilers supported the method.
--
Steve Clamage, stephen.clamage@eng.sun.com




Author: girod@dshp01.trs.ntc.nokia.com (Marc Girod)
Date: 20 Dec 1994 12:50:30 GMT
Raw View
>>>>> "Steve" == Steve Clamage <clamage@Eng.Sun.COM> writes:
In article <3d5jv3$qfc@engnews2.Eng.Sun.COM> clamage@Eng.Sun.COM (Steve Clamage) writes:

Steve> You'll have to be more explicit than that. Lippman's book contains
Steve> an example of declaring a set of templates as friends of a class,
Steve> but I never heard of a compiler which would compile that code. It
Steve> contradicted the stated rules for templates at that time. If in fact
Steve> there was a way to do it, I'd like to know about it, and about which
Steve> compilers supported the method.

??? I compiled the code I sent with HP CC: HP C++ B2402  A.03.40
Using HP cfront 3.55

I would believe I have had similar code with earlier versions though.

--------------------------------
class Z;
template <class T> class Y;
template <class T> void foo(T*);

class X {
  friend class Z;
  template <class T> friend class Y;
  template <class T> friend void foo(T*);
};

--------------------------------
--
+-----------------------------------------------------------------------------+
| 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     |
+-----------------------------------------------------------------------------+




Author: girod@dshp01.trs.ntc.nokia.com (Marc Girod)
Date: 19 Dec 1994 10:32:54 GMT
Raw View
In article <3cqlmi$4m1@engnews2.Eng.Sun.COM> clamage@Eng.Sun.COM (Steve Clamage) writes:

>>>>> "Steve" == Steve Clamage <clamage@Eng.Sun.COM> writes:

Steve> In article t0t@sun4.bham.ac.uk, imy@wcl-rs.bham.ac.uk (Ian Young) writes:
>> Should I be able to compile something like this? My compiler (a 1992
>> vintage of IBM's xlC) complains about "template" being unexpected in
>> this context.
>>
>> class OstreamWrapper {
>> ostream *ostr;
>> public:
>> OstreamWrapper(ostream& o) : ostr(&o) {}
>> ~OstreamWrapper();
>> friend template<class T> OstreamWrapper& operator<<(OstreamWrapper&, T);
>> };
>>
>> If this is now possible, was this change made at the same time as the
>> addition of member templates discussed in the thread "Declaring
>> templates in class scope"?

Steve> The language rule was always (and certainly in 1992) that
Steve> templates could be declared only at file scope.

Steve> This year (1994) the C++ Committee voted to allow member
Steve> templates. (There are some restrictions on them, and I would
Steve> not be surprised if more restrictions are added.)

Steve> You will have to check with your favorite compiler vendor to
Steve> find out when a compiler will be available that supports code
Steve> like the above.

I think there may have been a confusion...
Declaration of a family of templates as friend was possible earlier
already, with a slightly different syntax though (the placement of the
template keyword). It is simpler if the prototype was seen already. In
any case the template is not declared in the class scope through the
friend declaration, but in the ...(don't remember and was not trivial :-)


class OstreamWrapper {
    ostream *ostr;
  public:
    OstreamWrapper(ostream& o) : ostr(&o) {}
    ~OstreamWrapper();
  template <class T> friend OstreamWrapper& operator<<(OstreamWrapper&, T);
};


Same thing for:

class Z;
template <class T> class Y;
template <class T> void foo(T*);

class X {
  friend class Z;
  template <class T> friend class Y;
  template <class T> friend void foo(T*);
};
--
+-----------------------------------------------------------------------------+
| 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     |
+-----------------------------------------------------------------------------+




Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 16 Dec 1994 00:06:42 GMT
Raw View
In article t0t@sun4.bham.ac.uk, imy@wcl-rs.bham.ac.uk (Ian Young) writes:
>Should I be able to compile something like this? My compiler (a 1992
>vintage of IBM's xlC) complains about "template" being unexpected in
>this context.
>
>class OstreamWrapper {
>    ostream *ostr;
>public:
>    OstreamWrapper(ostream& o) : ostr(&o) {}
>    ~OstreamWrapper();
>    friend template<class T> OstreamWrapper& operator<<(OstreamWrapper&, T);
>};
>
>If this is now possible, was this change made at the same time as the
>addition of member templates discussed in the thread "Declaring
>templates in class scope"?

The language rule was always (and certainly in 1992) that templates could
be declared only at file scope.

This year (1994) the C++ Committee voted to allow member templates. (There
are some restrictions on them, and I would not be surprised if more
restrictions are added.)

You will have to check with your favorite compiler vendor to find out
when a compiler will be available that supports code like the above.

---
Steve Clamage, stephen.clamage@eng.sun.com