https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64216
Bug ID: 64216 Summary: Function template can access private sub class without being friend Product: gcc Version: 4.9.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: yyc1992 at gmail dot com The following code compiles on g++ 4.9.2 while it shouldn't. Accessing private members and access in non-templates are properly checked. However, a function template can actually access any private sub-class without being a friend. This problem exist for all standards I've tried ((gnu|c)++(98|03|11|14)) The code is properly rejected by clang++ 3.5.0. ``` // class C { struct T2 { }; static void f() {} }; template<typename T> static inline void f(T) { // C::f(); // This generates an error correctly C::T2 __attribute__((unused)) v2; // This does not generate an error } int main() { // C::T2 v2; // This also generates an error correctly f(1); return 0; } ```