When using templates, A<AI>::f should not be reachable from main() because it
is protected.
The correct behavior is observed in the template-less version: the compiler
detects an encapsulation error.

% cat foo.cpp
#if NO_TEMPLATES == 1
class A { protected: int f() { return 42; } };
class B : public A { using A::f; };
int main() { B b; return b.f(); }
#else
template<int AI> class A { protected: int f() { return AI; } };
template<int BI> class B : public A<BI> { using A<BI>::f; };
int main() { B<24> b; return b.f(); }
#endif

% g++ -DNO_TEMPLATES=1 -o foo foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:2: error: ‘int A::f()’ is protected
foo.cpp:4: error: within this context

% g++ -DNO_TEMPLATES=0 -o foo foo.cpp
% ./foo ; echo $?                    
24


-- 
           Summary: using with templates breaks encapsulation
           Product: gcc
           Version: 4.3.3
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: sebastien at soja dot org
 GCC build triplet: x86_64-linux-gnu
  GCC host triplet: x86_64-linux-gnu
GCC target triplet: x86_64-linux-gnu


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

Reply via email to