Topic: About granting friendship to a template parameter, still open question?


Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/04/06
Raw View
Jaakko =?UNKNOWN-8BIT?Q?J=E4rvi?= wrote:

[...]

> The reason I am not in favor of friendship to a template parameter
> is that the class template has NO idea what all classes have access to
> its private data. By which I mean, any class T, for which an
> instantiation X<T> exists, can make assumptions about the implementation
> details of the class X<T>. But the designer develops (or codes) class
> template X but not the instantiation X<T>. The compiler generates class
> instantiation X<T>. So, if the designer of the class template wants to
> change its implementation, he can not dare do it because he forgoes the
> concept of Data Abstraction at the class template level.

The reason why I disagree is that in my opinion, a template doesn't
*have* any members, and therefore no one can access them. However,
the classes which you get by instantiating it *are* aware of who
can access their friends.

Also note that
- your argument would hold for protected members as well (where you
  really don't know who will access your protected members)
- your argument perfectly holds for member template functions
  (which allow to access private parts of the class quite easily
  from arbitrary code, by simply specialising over an arbitrary
  (not otherwise related) class.
- If you add a friend declaration, you have a reason for this -
  a friend declaration IMHO is never put anywhere just by accident.
  Therefore the main reason to disallow something - accidental
  misuse - doesn't apply. (Remember that C++ is intended to protect
  against Murphy, not Machiavelli.)
---
[ 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              ]





Author: Hyman Rosen <uunet!jyacc!hymie@ncar.UCAR.EDU>
Date: 1998/03/25
Raw View
"Jaakko =?UNKNOWN-8BIT?Q?J=E4rvi"?= <jjarvi@cs.utu.fi> writes:
> A couple of weeks ago I posted the question
>
> "Why is this ill-formed? template<class T> class X { friend class T; ..."

Who says that this is ill-formed? For example, egcs compiles it
without complaint. I couldn't find anything in the CD2 that would
imply that this was not allowed. The compiler did complain when
I instantiated X with a non-class type, such as X<int>.
---
[ 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              ]





Author: "Jaakko =?UNKNOWN-8BIT?Q?J=E4rvi"?= <jjarvi@cs.utu.fi>
Date: 1998/03/26
Raw View
Hi, a quote from CD2 (the only one I have available):

7.1.5.3  Elaborated type specifiers                    [dcl.type.elab]
...
2 ...
  If the identifier (my addition: in friend class identifier) resolves to a
typedef-name or a template  type-parameter,  the  elaborated-type-specifier
is ill-formed.
  [Note: this implies that, within a  class  template  with  a  template
  type-parameter T, the declaration          friend class T;
  is  ill-formed.  ]

>> "Why is this ill-formed? template<class T> class X { friend class T; ..."
>
>Who says that this is ill-formed? For example, egcs compiles it
>without complaint. I couldn't find anything in the CD2 that would
>imply that this was not allowed. The compiler did complain when
>I instantiated X with a non-class type, such as X<int>.

< Jaakko Jdrvi, Computer Science, University of Turku, Finland
< Email: jaakko.jarvi@cs.utu.fi
---
[ 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              ]