From 2432993678782663218
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,5e0145d162a6d1d6
X-Google-Attributes: gidf78e5,public
From: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Subject: Re: Why is granting friendship to a templ. parameter ill-formed?
Date: 1998/03/13
Message-ID: <3508FF66.591FF69@physik.tu-muenchen.de>#1/1
X-Deja-AN: 333641337
Content-Transfer-Encoding: 7bit
References: <6dop9s$kq6$1@news.utu.fi> <35005B2A.7410@cs.wayne.edu> <1yiuppeaon.fsf@dshp01.ntc.nokia.com> <6e2i9u$rig$1@news.interlog.com> <350587D6.3EA6@cs.wayne.edu> <35069238.1C1DAA80@physik.tu-muenchen.de> <35082F4C.44C3@usa.net>
X-Original-Date: Fri, 13 Mar 1998 10:41:58 +0100
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Organization: [posted via] Leibniz-Rechenzentrum, Muenchen (Germany)
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUANQkJMeEDnX0m9pzZAQF9QAF+KQyJ7QpEVYEwK5hm+sTo73dFIM5HG8p5 iX6jK/lHkt0VVkdsUViAY78EjT3hTU96 =SUCJ
Newsgroups: comp.std.c++


Srinivas Vobilisetti wrote:
> 
> Christopher Eltschka wrote:
> >
> > Srinivas Vobilisetti wrote:
> > >
> > > Daniel Parker wrote:
> > > >
> > > > Marc Girod wrote in message <1yiuppeaon.fsf@dshp01.ntc.nokia.com>...
> > > > >>>>>> "SV" == Srinivas Vobilisetti <srv@cs.wayne.edu> writes:
> > > > >
> > > > >SV> To prevent back door access to template class's private members.
> > > > >SV> If you give friendship based on the template parameter, then any
> > > > >SV> class can get access to the template private members thru back door
> > > > >
> > > > >"Back door"??? What is the front door then?
> > >
> > > Front door? List all the classes as friends in the template class
> > > definition.
> >
> > But that is not quite the same:
> >
> > With friend class <Template-Param>, you have Y is friend of X<Y>
> > and Z is friend of X<Z>, but *not* Y is friend of X<Z>.
> >
> > Also, it *doesn't* open any back door. To get to the private
> > members of X<Y>, I can't define another class Z, instantiate an X<Z>,
> > and thus access X<Y>'s private data. I can, of course, access the
> > private members of X<Y> from Y - but that's what the friend
> > declaration is for, and the reason to put it there in the first place
> > (nobody puts it there because it just looks cool).
> > If a template parameter is declared friend, it is obviously thought
> > as working together with that class.
> >
> > What *does* open a back door to the template class members are
> > templated member functions (which are indeed allowed). For example:
> >
> 
> I did not put it correctly, when I said "List all the classes as friends
> in the template class definition". What actually I meant is something
> like the following:
> 
> class A {
> };
> 
> class B {
> };
> 
> class C {
> };
> 
> class D {
> };
> 
> template <class T> class X {
> 
> friend class T = A, B, C;
> 
> // [...]
> };
> 
> The statement "friend class T = A, B, C;" means class A is friend of
> class X<A>, class B is friend of class X<B> and class C is friend of
> class X<C> but class D is not a friend of class X<D>. It also means
> class A is not a friend of class X<S != A>.
> 
> By doing this, template class X knows what all classes have access to
> its private members. Otherwise, template classes will turn out to be a
> maintenance nightmare.

But by doing this (is this really legal syntax?), you just
pervert the meaning of templates. A template is, well, a template,
from which you can get several classes with the same code (unless
specialized). Your template class indeed encloses three
specialisations: One for X<A>, one for X<B> and one for X<C>.
In effect, it may be absolutely useless for instantiation for
*any* other class (there's obviously a reason why there's a friend
declaration). Therefore you most probably don't have a full-blown
template, but a "restricted" template that is only useful for just
those three classes.

Also note that there is *not* a thing like a "template class"
(although this term is used often), but rather a class template.
That is, the template itself is not a class, and therefore
does not grant friendship to anyone. However, the instatiations
of the template are classes, each independant of the other.
And if the template has a friend declaration for a template
parameter, that means that a generated class should have a friend
declaration to the class specified on instantiation.

You didn't answer the last sentence of my post (which is indeed
the most important one to decide if you are right or not):

---8<---
Then please post an example of accidental misuse of such a friend
declaration for a template parameter.
---8<---
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]



