Topic: Nested template classes
Author: dbkruger@ix.netcom.com (Dov Kruger )
Date: 1996/06/20 Raw View
Nested classes are explicitly declared legal in the ARM.
Template classes are legal as well.
But there is no mention that I can find about nested template classes.
If I write a nested class that contains another class:
template <class T>
class List {
private:
T* data_;
size_t size_;
public:
class Iter {
private:
T* current_;
public:
void B(A& a) { current_ = a.data_; }
};
friend Iter;
};
The inside class is not allowed to refer to private components of the
outside class without being declared as a friend.
Where does the standard state the friend declaration should be?
g++ allows me to declare the friend after the body of iter,
just before the end of class List. That is a retroactive
declaration, but it works for gnu.
Many other compilers don't like that, but neither do they like the
declaration to come before:
template <class T>
class List {
private:
T* data_;
size_t size_;
public:
friend class Iter;
class Iter {
private:
T* current_;
public:
void B(A& a) { current_ = a.data_; }
};
};
Many compilers consider this second to be declaring a class on the
outside to be a friend. ie ::Iter, not List<T>::Iter
I have also tried to do it as I define the class:
friend class Iter {
private:
So, what's right?
---
[ 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
]