Template friends in class templates.

Template friends in class templates.

Post by Kevin S. Van Hor » Thu, 24 Jul 1997 04:00:00



I was recently reading over the Dec. '96 Working Paper, and ran
across something that looked a bit peculiar.  The WP states that a
*non-template* class can have a friend template, i.e. you can write
things like

  class X {
     template <class T> friend class B;
     template <class T> friend void foo(T const & x);
     ...
  };

But apparently you can't do this for a class template, i.e.

  template ,class U>
  class X {
     template <class T> friend class B;
  };

is illegal.  I couldn't find anyplace where the WP explicitly *said*
this was illegal, but it only explicitly allowed friend templates for
non-template classes.

Does anyone know why this restriction exists?  It seems to me that it
weakens the whole idea of templates.  It means that there are certain
classes (those that have friend templates) that *cannot be generalized*
by abstracting over one or more of the types used in them.

Even for a non-template class, there appears to be a serious limitation
on what kind of friend templates you can have.  I'm not entirely sure of
this, but it looks as if you can have a friend declaration that looks
like
   template <class T, class U> friend class C;
but you can't do something like this:
   template <class U> friend class C<int, U>;

Can anyone comment on this?  Why all these restrictions?

-----------------------------------------------------------------

KSVH Software and Consulting | http://www.xmission.com/~ksvhsoft/
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]

[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]

 
 
 

Template friends in class templates.

Post by Kresimir Fres » Thu, 31 Jul 1997 04:00:00



Quote:

> I was recently reading over the Dec. '96 Working Paper, and ran
> across something that looked a bit peculiar.  The WP states that a
> *non-template* class can have a friend template, i.e. you can write
> things like

>   class X {
>      template <class T> friend class B;
>      template <class T> friend void foo(T const & x);
>      ...
>   };

> But apparently you can't do this for a class template, i.e.

>   template ,class U>

    template <class U>
Quote:>   class X {
>      template <class T> friend class B;
>   };

> is illegal.

[...]

AFAIK, it's legal. But I don't know why this is not explicitly
said. I guess it can be somehow deduced from other rules
(not just in subsection 14.5.3) and from language grammar.
Maybe some kind soul can throw some light on this? (Daveed?)

At least one compiler I know of, which claims that it is
``the result of serious tracking of the ANSI/ISO C++ committee
deliberations to date'', supports this (Comeau C++ 4.0
Pre-Release, `http://www.comeaucomputing.com/'). And I think
Kai compiler too, `http://www.kai.com/C_plus_plus/'.

Also, there were some discussion on `friend templates' in
this newsgroup and in c.l.c++.mod. Try DejaNews.

Quote:> Even for a non-template class, there appears to be a serious
> limitation on what kind of friend templates you can have.  
> I'm not entirely sure of this, but it looks as if you can
> have a friend declaration that looks like
>    template <class T, class U> friend class C;
> but you can't do something like this:
>    template <class U> friend class C<int, U>;

Correct syntax is (at least for Comeau compiler):

  class A {
    template <class U, int i> friend class F;
  };

  template <class T>
  class B {
    template <class U, int i> friend class F;  
  };

fres


---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]

[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]


 
 
 

Template friends in class templates.

Post by David Vandevoord » Fri, 01 Aug 1997 04:00:00




[...]
> > But apparently you can't do this for a class template, i.e.

> >   template ,class U>

>     template <class U>
> >   class X {
> >      template <class T> friend class B;
> >   };

> > is illegal.
> [...]

> AFAIK, it's legal. But I don't know why this is not explicitly
> said. I guess it can be somehow deduced from other rules
> (not just in subsection 14.5.3) and from language grammar.
> Maybe some kind soul can throw some light on this? (Daveed?)

Hmmm... I'm pretty sure that the intention is for it to be
valid. (There are examples of this usage in a J16 internal
paper that tracks open template issues.) However, you seem
to be correct about the precise WP wording; I suspect it's
an oversight.

        Daveed
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your

  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html

]

 
 
 

1. How to make a general template class friend of a non-template class?

Hi everybody,
I have the need to declare a non-template class which specifies a
template
class ( with a general template parameter T) as its friend for any
kind of T
I'd like to use in the further instantiation of the template class.
The code would be something like :

template <class T>
class TemplateClass{
.......
.......
.......

class NonTemplateClass{
    friend class TemplateClass<int>; // it works fine 'cause T=int, as

                                 //any other previously defined UDT
                                 // also, is a known type

    friend class TemplateClass<T>;  // it doesn't work 'cause the
                                 // compiler doesn't know T yet
                                //  *** but this is what I need,any
                                // kind of T ***
.......
.......
.......

I know the sintax is wrong, because T is not in scope in class
NonTemplate,
although I need to have TemplateClass<T> to be a friend of
NonTemplateClass whatever T is.
Is it possible to have such a relationship, and how?

Thanks in advance, and my best regards

Vittorio Digilio
Sisdata srl

2. Problem with Sheet and IF command.

3. friend template class in template class

4. may one install VS.Net 2003 on more than one of one's PC's?

5. Template class as the friend of a template class

6. Newbie question: Hooking an Airport Extreme station to a DSL connection?

7. friend templates and template friends

8. CD player?

9. use of a template function that is friend to a template class

10. Friend function template of class template

11. Syntax for declaring friend function template in class template

12. Template Functions as Friends of Template Classes

13. template friend function of a template class