Topic: nested classes in class template
Author: "Artem Bezrodnov" <darth@pt.comcor.ru>
Date: Fri, 30 Mar 2001 19:28:44 GMT Raw View
Dear all,
I would like to ask another question: suppose the following code is given:
template <class T> class Wrapper {
protected:
class B;
friend class B;
class A {
int m_ia;
};
class B {
virtual int f(A& _refA);
};
};
template <class T>
inline int Wrapper::B::f(A& _refA) { // line 17
return 0;
}
int main() {
Wrapper<int> w;
return 0;
}
Again, this code seems to compile fine with GCC 2.95.2, but unfortunately
makes Sun WS6U1 C++ compiler say the following:
"line 17: Error: Wrapper<T>::A is not accessible from Wrapper<T>::B"
Removing "templateness" from class Wrapper removes the problem as well.
Defining function f inside class B also clears the issue. I couldn't find
any contradiction with December 1996 draft of C++ Standard, so my question
follows: is there any contradiction of the given code with the final
ANSI/ISO C++ Standard (unfortunately, I still don't have a copy of that...)?
Thanks in advance for any feedback!
Artem
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Andrei Iltchenko" <iltchenko@yahoo.com>
Date: Sun, 1 Apr 2001 02:42:43 GMT Raw View
Artem Bezrodnov <darth@pt.comcor.ru> wrote in message
news:9a1mmj$mq3$1@newsflood.tokyo.att.ne.jp...
> Dear all,
>
> I would like to ask another question: suppose the following code is given:
>
> template <class T> class Wrapper {
> protected:
> class B;
> friend class B;
>
> class A {
> int m_ia;
> };
> class B {
> virtual int f(A& _refA);
> };
> };
>
> template <class T>
> inline int Wrapper::B::f(A& _refA) { // line 17
> return 0;
> }
>
> int main() {
> Wrapper<int> w;
> return 0;
> }
>
> Again, this code seems to compile fine with GCC 2.95.2, but unfortunately
> makes Sun WS6U1 C++ compiler say the following:
> "line 17: Error: Wrapper<T>::A is not accessible from Wrapper<T>::B"
This time the code shouldn't compile indeed. For two reasons:
1. The definition of the member function of the nested class of the class
template Wrapper is ill-formed. You should've written:
template <class T>
inline int Wrapper<T>::B::f(A& _refA) {
return 0;
}
2. According to the current version of ISO/IEC C++ Standard, members of the
nested class B have no access to protected members of the enclosing class
template Wrapper, thence:
> template <class T> class Wrapper {
> protected:
> class B;
> friend class B;
>
> class A {
> int m_ia;
> };
> class B {
> virtual int f(A& _refA);
The declaration above is ill-formed (the same of course stands for the
corresponding definition), as f, being a member of B, has no access to the
protected member A of its enclosing class template Wrapper! See chapter 11.8
of your draft version of the Standard, as the final version has left that
chapter intact.
> };
> };
Note, however, that none of the compilers that I have access to complained
about the name A being inaccessible, and that's because of the fact that
members of nested classes are not considered members of the enclosing class
for the purposes of member access control leads to a number of issues. As a
result, WG21 & J16 are about to change the wording of the relevant
paragraphs of chapter 11 in the next revision of the Standard to allow for
the above member access:
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_active.html#45
And the major compiler vendors seem to have taken note of the proposed
resolution.
Sincerely,
Andrei Iltchenko.
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]