Topic: templates/friends


Author: maxtal@Physics.usyd.edu.au (John Max Skaller)
Date: 1995/05/22
Raw View
In article <5m5kMRHsG3B@couch.ping.de>, Thomas Arend <TA@couch.ping.de> wrote:
>Hi everybody,
>
>
>this is another templates/friends question. Suppose there are two
>templated classes: A and B.
>
>Now, A uses instances of B, for example
>
>  template<class T1, class T2>
>  class A
>  {
>    B<T1> array[15] ;
>    // further stuff
>  } ;
>
>A possesses memberfunctions which (try to) use private/protected features
>of B, so A may be declared as a friend of B. If there is only one template-
>parameter for A this might be quite simple:
>
>  template<class T3>
>  class B
>  {
>  friend class A<T3> ; // accepted by g++ 2.6.3
>  /* ... */
>  } ;
>
>What is the correct way to do so if A is fitted with more than one
>parameter?

 (like ..

>  template<class T3>
>  class B
>  {
>  friend class A<T3,int>    ; // specify the second template-argument.
>  friend class A<T3,double> ;
>  /* proper repeated to infinity */
>  } ;
>
>In fact I use this workaround, but there should be a better solution.

 A friend can be a class, and perhaps it can be
a template. However there is NO ENTITY

        template<class T2> class A<T1,T2>

which you can name as a friend.  This thing is neither a template
nor a class. The "unified model" of the ARM has been discarded in favour
of a model in which each template has discrete and distinct identity.

The Standard may end up(**)
permitting a partial specialisation as a friend,
but it isn't clear that would refer to any more than an
explicitly given partial specialisation definition.

The reason this is problematic while the equivalent case
of full specialisation is not, is that there can be at
most ONE full specialisation of a template for given
arguments.

However, any number of explicit partial specialisations
can be given in which the argument vectors overlap.
It is not clear whether a friend declaration naming
a partial specialisation would be granting access
to a particular explicit specialisation, or to
ANY full specialisation which matched. For example:

 template<class T, class U> class X{ .. }
 template<class T, class U> class X<T, U*> { .. }
 template<class T> class X<T, T*> { .. }
 ...
 friend template<class T> class X<T,U> { .. }
 ...
 X<int,int*> xx;

Is this instance permitted access by the friend declaration?
It is surely a case of X<T,U*> but it is not actually
generated by that partial specialisation, but by X<T,T*>.


(**) this is currently an open issue.

--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,
        81A Glebe Point Rd, GLEBE   Mem: SA IT/9/22,SC22/WG21
        NSW 2037, AUSTRALIA     Phone: 61-2-566-2189





Author: TA@couch.ping.de (Thomas Arend)
Date: 1995/05/18
Raw View
Hi everybody,


this is another templates/friends question. Suppose there are two
templated classes: A and B.

Now, A uses instances of B, for example

  template<class T1, class T2>
  class A
  {
    B<T1> array[15] ;
    // further stuff
  } ;

A possesses memberfunctions which (try to) use private/protected features
of B, so A may be declared as a friend of B. If there is only one template-
parameter for A this might be quite simple:

  template<class T3>
  class B
  {
  friend class A<T3> ; // accepted by g++ 2.6.3
  /* ... */
  } ;

What is the correct way to do so if A is fitted with more than one
parameter? To be precise: what is claimed by THE DRAFT and what might be
accepted by g++ 2.6.3? By the way, g++ can be 'tricked' out by something
like

  template<class T3>
  class B
  {
  friend class A<T3,int>    ; // specify the second template-argument.
  friend class A<T3,double> ;
  /* proper repeated to infinity */
  } ;

In fact I use this workaround, but there should be a better solution.
There is absolutely no need for class B to know anything about the further
arguments of class A.

Before you answer: please refer to THE DRAFT or post a solution which is
definitely working (!!) (on g++ prefered). Don't tell me what >should<
work since g++ is not (much) interested in what I like him/her/it to do.

Thanks in advance,

        Thomas


  -------------------------------------------------------------------
Thomas Arend      blasius@chemietechnik.uni-dortmund.de  (office)
                  ta@couch.ping.de                       (private)

## CrossPoint v3.02 ##