Topic: Templates, friends and r.14.7
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1995/05/18 Raw View
patil@bnr.ca (Vinayak Patilkulkarni) writes:
>I have a problem declaring a templated class as friend. C++ programming
>language does not seem to handle it.
Bingo.
However, Cfront has always allowed friend templates, and I think
the committee does want to allow this sort of thing.
The current draft is somewhat schitzophrenic on the issue.
>class QElem {
>private :
> void stamp () {}
> friend class Q;
> // What i intend to do here is to make stamp()
> // as friend function of all Q<T> classes.
To do that, you would have to write
class QElem {
private :
void stamp () {}
template <class T> friend class Q<T>;
However, as noted above this usage is somewhat dubious, and is
not supported by some compilers.
> // ObjectCenter compiler takes the above line and makes
> // stamp () as friend function of all types of Q.
> // However g++ compiler flags the error. (gnu g++ 2.6.2)
ObjectCenter is wrong. There is a big difference between a template-name
and a class-name. In your example, `Q' is a template-name, not a class-name.
>But it does not solve my problems, as my library is expected to run
>with different compliers.
In that case, don't use template friends.
Just make QElem::stamp() public (with some appropriate documentation).
--
Fergus Henderson | I'll forgive even GNU emacs as
fjh@cs.mu.oz.au | long as gcc is available ;-)
http://www.cs.mu.oz.au/~fjh | - Linus Torvalds
Author: patil@bnr.ca (Vinayak Patilkulkarni)
Date: 1995/05/12 Raw View
Hi,
I have a problem declaring a templated class as friend. C++ programming
language does not seem to handle it.
Consider the simple example given below.
#include <iostream.h>
class QElem {
private :
void stamp () {}
friend class Q;
// What i intend to do here is to make stamp()
// as friend function of all Q<T> classes.
// ObjectCenter compiler takes the above line and makes
// stamp () as friend function of all types of Q.
// However g++ compiler flags the error. (gnu g++ 2.6.2)
};
template <class T> class Q {
public:
void add (T* p) { p->stamp (); }
};
class A : public QElem {};
void main ()
{
Q<A> x;
x.add (new A);
}
Now the question is which one is correct ?
ObjectCenter
or GNU ?
or It is not defined been handled by the language.
r.14.7 in Reference manual of C++ programming lang II edition does not
list such an example.
I am happy to accept the feature (or bug) provided by ObjectCenter CC.
But it does not solve my problems, as my library is expected to run
with different compliers.
- Patil
#opinions are mine only.