Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can a templatized derived class access its base private members on gcc?

I'm in the process of learning c++. Now i understand that a derived class cannot access its base class private members, but why a templatized one can ?

for instance something like this works fine:

class base {
     static int x;
};
template<typename T>
class derived: public base{
    T t;
public:
    void setx(int i) {x=i;}
    int getx(){return x;}
};

I'm using gcc 5.4 on linux.

like image 609
s.clarck Avatar asked Apr 05 '18 15:04

s.clarck


1 Answers

This is a known bug of GCC, which seems to fail in performing access checking correctly in templates. See Bug 58740.

Unfortunately it's still not fixed.

BTW: Clang fails to compile, as expected.

like image 105
songyuanyao Avatar answered Oct 20 '22 01:10

songyuanyao