Topic: Template friends in class templates.
Author: David Vandevoorde <daveed@vandevoorde.com>
Date: 1997/07/31 Raw View
Kresimir Fresl wrote:
>
> Kevin S. Van Horn wrote:
[...]
> > 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
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: Kresimir Fresl <fresl@grad.hr>
Date: 1997/07/30 Raw View
Kevin S. Van Horn wrote:
>
> 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>
> 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.
> 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
[e-mail: fresl@grad.hr]
---
[ 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: "Kevin S. Van Horn" <kevin.s.vanhorn@iname.com>
Date: 1997/07/23 Raw View
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?
-----------------------------------------------------------------
Kevin S. Van Horn | kevin.s.vanhorn@iname.com
KSVH Software and Consulting | http://www.xmission.com/~ksvhsoft/
---
[ 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 ]