The code below compiles, but it should give an error, because the Policy ctor is not accessible when inheriting protected from Policy To have valid C++ code the inheritance must be public. struct Policy { protected: Policy() {} Policy(const Policy&) {} }; template<int I, class P> struct BugGcc : protected P //public P { BugGcc() {} template<int I, class P> BugGcc(const BugGcc<I, P>& t) : P(t) {} }; void foo() { BugGcc<0, Policy> f1; BugGcc<1, Policy> f2(f1); } GCC: gcc-Version 4.3.2 20081105 (Red Hat 4.3.2-7) (GCC)
Confirmed, related to PR 26693. Here is the correct testcase (the testcase below does not compile with 4.4 but for a different reason than the real bug): struct Policy { protected: Policy() {} Policy(const Policy&) {} }; template<int I, class P> struct BugGcc : protected P //public P { BugGcc() {} template<int I1, class P1> BugGcc(const BugGcc<I1, P1>& t) : P(t) {} }; void foo() { BugGcc<0, Policy> f1; BugGcc<1, Policy> f2(f1); }
11.2: If a class is declared to be a base class for another class using the protected access specifier, the public and protected members of the base class are accessible as protected members of the derived class. Not a bug.
11.2 is talking about a different case. When you instantiate the integer template parameter manually you will see that it is really a bug: struct Policy { protected: Policy() {} Policy(const Policy&) {} }; template<class P> struct BugGcc0 : protected P //public P { BugGcc0() {} }; template<class P> struct BugGcc1 : public P { BugGcc1() {} template<class P1> BugGcc1(const BugGcc0<P1>& t) : P(t) {} }; void foo() { BugGcc0<Policy> d0; BugGcc1<Policy> d1(d0); } The Policy ctor is visible within BugGcc1 (because it is inherited protected) but it is not visible to a different class (again, because it is inherited protected). BugGcc0 and BugGcc1 only have the same base class but they are NOT of same type which 11.2 talks about. The protected policy ctor is only visible for BugGcc0 but not for BugGcc1.
It has nothing to do with templates. This code still compiles: struct P { protected: P() {} P(const P&) {} }; struct B : protected P { B() {} }; struct C : public P { C(const B& b) : P(b) {} }; void foo() { B b; C c(b); //P p(b); // <-- compiler error } But I it should not, because only within the scope of B B "is a" P. Globally B "is not a" P. Therefore you can't pass a instance of B in a scope different to B to the constructor of P. Even when C inherits from P the struct B is still "not a" P in the scope of C.
Ah yes, I see. The bug is not with the visibility of the copy ctor, but with the conversion from B to P in order to call it.
This bug was introduced by the fix for PR 10990.
Subject: Bug 38579 Author: jason Date: Fri Jan 16 18:35:28 2009 New Revision: 143439 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=143439 Log: PR c++/38579 * search.c (protected_accessible_p): N doesn't vary. Added: trunk/gcc/testsuite/g++.dg/conversion/access1.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/search.c trunk/gcc/testsuite/ChangeLog
Fixed for 4.4, not applying to older branches.
*** Bug 35640 has been marked as a duplicate of this bug. ***
Note that this will be broken again in 4.9.4 and 5.3, because the fix caused the worse bug 66957 and so I reverted it on those branches. 6.0 will have a proper fix.