Reading specs from /usr/gcc3local/lib/gcc-lib/i686-pc-linux-gnu/3.3/specs Configured with: ../gcc-3.3/configure --prefix=/usr/gcc3local --enable-languages=c,c++ Thread model: posix gcc version 3.3 With the following code: class A { protected: void foo() {} public: A(); }; class B : public A { void bar() { A a; a.foo(); void (A::*pmf)() = &A::foo; // 1 (a.*pmf)(); // 2 } }; Compilation gives: g++ -W -Wall -ansi -pedantic -c pmf.cpp pmf.cpp: In member function `void B::bar()': pmf.cpp:3: error: `void A::foo()' is protected pmf.cpp:12: error: within this context Compilation exited abnormally with code 1 at Thu Jun 12 10:28:26 But note that I am able to call the protected member function of the external object via a pointer to member function (PMF). Looking at the C++ spec (11.5 example see void D2::mem...) I think the line labelled 1 should be ill-formed but since the compiler doesn't think so I am able to gain access to the protected function on the line labelled 2.
Confirmed. Reduced snippet: --------------------------------- struct A { protected: int a; }; struct B : A { void foo() { (void)&A::a; } }; --------------------------------- This is accepted by any version of GCC but it is ill-formed (see the example in ยง11.5).
Patch submitted: http://gcc.gnu.org/ml/gcc-patches/2003-08/msg00998.html
Subject: Bug 11174 CVSROOT: /cvs/gcc Module name: gcc Changes by: lerdsuwa@gcc.gnu.org 2003-08-19 12:55:00 Modified files: gcc/cp : ChangeLog init.c gcc/testsuite : ChangeLog gcc/testsuite/g++.old-deja/g++.jason: access17.C Added files: gcc/testsuite/g++.dg/parse: access4.C access5.C Log message: PR c++/11174 * init.c (build_offset_ref): Perform access checking for pointer to member correctly. * g++.dg/parse/access4.C: New test. * g++.dg/parse/access5.C: Likewise. * g++.old-deja/g++.jason/access17.C: Adjust error message. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.3608&r2=1.3609 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/init.c.diff?cvsroot=gcc&r1=1.339&r2=1.340 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.2976&r2=1.2977 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/parse/access4.C.diff?cvsroot=gcc&r1=NONE&r2=1.1 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/parse/access5.C.diff?cvsroot=gcc&r1=NONE&r2=1.1 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.old-deja/g++.jason/access17.C.diff?cvsroot=gcc&r1=1.5&r2=1.6
Fixed in the main trunk.
*** Bug 12119 has been marked as a duplicate of this bug. ***
*** Bug 14894 has been marked as a duplicate of this bug. ***
*** Bug 15308 has been marked as a duplicate of this bug. ***
*** Bug 15468 has been marked as a duplicate of this bug. ***
Reopening for documentation.
I'll take care of writing up changes.html/bugs.html for this. It affects more than pointer-to-members, and appears to be a FAQ.
Closing this one as PR 14949 is the one for the documenation.