Topic: Friends & Templates


Author: Greg Silverman <greg_silverman@smtp.svl.trw.com>
Date: 1996/06/12
Raw View
I understand the reasoning behind denying inheritance of friendship.
But, why is friendship denied to template classes? I could not do
the following

class A {
  friend template <class T> class X;
};

The compiler said that friendship could only be granted to a class and
that X is not a class but many classes. Does the standard backup the
compiler (Solaris 4.1) on this?

Giving friendship to a group of classes through templatizing is
different from giving it to a group of classes through inheritance.
Inheritance would allow a subclass to violate the friendship priviledge
by doing something it should not. However, when a template is
instantiated with classes there would not be any opporutinty for the
actual classes to violate the priviledge. In

template <class T>
class A {
....
};

all use of friendship of A by X is hidden from T. Isn't it? I can't see
how T could be written to know about A's protected/private members and
functions and abuse them. Am I wrong?

Greg Silverman
TRW/Sunnyvale, Ca


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: vandevod@cs.rpi.edu (David Vandevoorde)
Date: 1996/06/13
Raw View
>>>>> "GS" == Greg Silverman <greg_silverman@smtp.svl.trw.com> writes:

GS> I understand the reasoning behind denying inheritance of friendship.
GS> But, why is friendship denied to template classes? I could not do
GS> the following

GS> class A {
GS>   friend template <class T> class X;
GS> };

GS> The compiler said that friendship could only be granted to a class and
GS> that X is not a class but many classes. Does the standard backup the
GS> compiler (Solaris 4.1) on this?

Sort of. The correct syntax is:

 class A {
    template<class T> friend class X;
 };

Edison Design Group's C++ front-end version 2.31 (perhaps also earlier
ones) supports this. However, it is a quite recent addition to the
language and your compiler may not have tracked this change yet.

 Daveed
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: anhaeupl@late.e-technik.uni-erlangen.de (Bernd Anhaeupl)
Date: 1996/06/13
Raw View
In article <31BF246B.7F74@smtp.svl.trw.com> Greg Silverman
<greg_silverman@smtp.svl.trw.com> writes:

   I understand the reasoning behind denying inheritance of friendship.
   But, why is friendship denied to template classes? I could not do
   the following

   class A {
     friend template <class T> class X;
   };

   The compiler said that friendship could only be granted to a class and
   that X is not a class but many classes. Does the standard backup the
   compiler (Solaris 4.1) on this?

Look at 14.12 of the (public) draft. There is an explicit example of
friend templates:

 class A {
  template <class T> friend B;      //ok
  template <class T> friend void f(T);   //ok

  template <class T> friend BB { /* ... */ } // error
  template <class T> friend void ff(T) { /* ... */ } // error
 };

So you can declare a friend template of a class but you must not define
it inside the class.
--
Bernd Anhaeupl   Tel.:  +49 9131 857787
LATE - Uni Erlangen
Cauerstr. 7   Email: anhaeupl@late.e-technik.uni-erlangen.de
91058 Erlangen
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: ball@Eng.sun.com (Mike Ball)
Date: 1996/06/13
Raw View
In article bma@rznews.rrze.uni-erlangen.de, anhaeupl@late.e-technik.uni-erlangen.de (Bernd Anhaeupl) writes:
> In article <31BF246B.7F74@smtp.svl.trw.com> Greg Silverman
>
> So you can declare a friend template of a class but you must not define
> it inside the class.

However, something being in the draft is not the same is it's being in a
particular compiler, and I happen to know that this feature is not in
the SunSoft C++4.1 compiler.

Being in one particular draft is not the same is being in some later draft,
by the way.  There is a fair amount of volitility in some areas.

---
Michael Ball
SunSoft Development Products




[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: jason@cygnus.com (Jason Merrill)
Date: 1996/06/13
Raw View
>>>>> Bernd Anhaeupl <anhaeupl@late.e-technik.uni-erlangen.de> writes:

> Look at 14.12 of the (public) draft. There is an explicit example of
> friend templates:

>  class A {
>   template <class T> friend B;      //ok
>   template <class T> friend void f(T);   //ok

>   template <class T> friend BB { /* ... */ } // error
>   template <class T> friend void ff(T) { /* ... */ } // error
>  };

> So you can declare a friend template of a class but you must not define
> it inside the class.

That has changed in the current (May) draft:

2 A friend template may be declared  within  a  non-template  class.   A
  friend  function  template may be defined within a non-template class.
  In these cases, all specializations of the class or function  template
  are friends of the class granting friendship.  [Example:
          class A {
                  template<class T> friend class B; // ok
                  template<class T> friend void f(T){ /* ... */ } // ok
          };
   --end example]

Jason


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]