Derived classes may access protected members of their base class only on
themselves or other instances of the same type or derived type. Here is a link
to the relevant section in the draft of the C++ standard:

http://www.csci.csusb.edu/dick/c++std/cd2/access.html#class.protected

I don't have access to the actual standard, but I figure you do :-).

The bug occurs when the protected member is a template member function. If the
protected member is a simple data member the code fails to compile as it
should.

BTW, VC++ 2005 catches this bug and refuses to compile the test program.

Test Program (bug.cpp)
----------------------
struct B
{
protected:
  template <typename T>
  T getX()
  {
    return T(x);
  }
protected:
  int x;
};

struct D : public B
{
  D(B * b)
  {
    //x = b->x;         // fails to compile (correct)
    x = b->getX<int>(); // doesn't fail to compile (bug)
  }
};

int main (int argc, char * const argv[]) 
{
  B b;
  D d(&b);
}

Output of gcc -v
----------------
Using built-in specs.
Target: x86_64-unknown-linux-gnu
Configured with: ../src/gcc-4.0.2/configure  : (reconfigured)
../src/gcc-4.0.2/configure 
Thread model: posix
gcc version 4.0.2

Build command
-------------
g++ -Wall bug.cpp -o bug


-- 
           Summary: g++ allows access to protected template member functions
                    of base class
           Product: gcc
           Version: 4.0.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: gsayfan at numenta dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32519

Reply via email to