This is the mail archive of the
gcc-prs@gcc.gnu.org
mailing list for the GCC project.
c++/7818: Bug collection for unbound template friend member functions that cannot access protected members (gcc 3.x)
- From: David Decotigny <David dot Decotigny at irisa dot fr>
- To: gcc-gnats at gcc dot gnu dot org
- Cc: ddecotig at irisa dot fr
- Date: Tue, 3 Sep 2002 14:42:26 +0200
- Subject: c++/7818: Bug collection for unbound template friend member functions that cannot access protected members (gcc 3.x)
>Number: 7818
>Category: c++
>Synopsis: protected members still non-reachable from an unbound friend template member function
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: rejects-legal
>Submitter-Id: net
>Arrival-Date: Tue Sep 03 05:46:01 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator: David Decotigny
>Release: 3.1
>Organization:
IRISA, Campus de Beaulieu, 35042 Rennes Cedex, FRANCE
>Environment:
System: SunOS blutch 5.7 Generic_106541-19 sun4u sparc SUNW,Ultra-5_10
Architecture: sun4
host: sparc-sun-solaris2.7
build: sparc-sun-solaris2.7
target: sparc-sun-solaris2.7
configured with: ../gcc/gcc-3.1/configure --prefix /usr/local/gcc-3.1 --enable-shared --enable-languages=c++,f77
>Description:
3 problems.
1/ When a friend member function F() of an unbound template class
T<class Bar> is declared inside a template class C<class Foo>, gcc
forbids access to the protected members of C<Foo> from
T<Bar>::F(). Work-around: declare an additional unbound friend class
U<class Baz> of C<Foo>, with a member function U<Baz>::Toto() that
accesses the protected members of C<Foo>, with the same "Foo" for T
and U.
2/ If T binds C<Foo> with Foo = Zorglub1, and U binds it with a
different Foo = Zorglub2, then none of the previous configurations
suite gcc: the same error for both T<Bar>::F() and U<Baz>::Toto(),
complaining that the member they try to access is protected. No
work-around found.
3/ If class C is not a template class anymore, then same symptoms. No
work-around found.
This behavior is common to gcc 3.0, 3.0.1, 3.0.4 and 3.1 at least
(solaris 2.7/sparc64). gcc-2.95.3 always fails (no work-around found,
even for 1/).
>How-To-Repeat:
See comments at the top of the file:
--------------------------------------------------------
// Bug 1: undefine macro HIDE_ERROR
// Bug 2: Bind "TheClass<short> c" with something different then
// "short" in bar() or member() (but not both)
// Bug 3: replace "template <class Foo> class TheClass" with "class
// TheClass" (ie no template) and update the "TheClass<short> c"
// accordingly
// Forward declaration
template <class T> class AnotherFriend;
template <class Foo>
class TheClass
{
public:
protected:
int prot_data;
template <class Baz>
friend class Friend; // unbound friend class
template <class Bar> // unbound friend member function
friend void AnotherFriend<Bar>::member();
};
#ifdef HIDE_ERROR
template <class Baz>
class Friend
{
public:
void bar()
{
TheClass<short> c;
c.prot_data = 2;
}
};
#endif
template <class Bar>
class AnotherFriend
{
public:
void member()
{
TheClass<short> c;
c.prot_data = 2;
}
};
int f()
{
#ifdef HIDE_ERROR
Friend<char> t;
t.bar();
#endif
AnotherFriend<int> ff;
ff.member();
return 0;
}
--------------------------------------------------------
Here is how it looks like:
[blutch] /tmp/ddecotig >g++-3.1 -DHIDE_ERROR -c a.cc
# Ok
[blutch] /tmp/ddecotig >g++-3.1 -c a.cc
a.cc: In member function `void AnotherFriend<T>::member() [with Bar = int]':
a.cc:59: instantiated from here
a.cc:16: `int TheClass<short int>::prot_data' is protected
a.cc:47: within this context
zsh: exit 1 g++-3.1 -c a.cc
>Fix:
>Release-Note:
>Audit-Trail:
>Unformatted: