http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59151
Bug ID: 59151 Summary: gcc name lookup for friend template finds class outside of Product: gcc Version: 4.7.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: gafunchal at gmail dot com template<typename T> class Class1; namespace B { template<typename U> class Class2 { template<typename V> friend class Class1; private: int something_private; }; } template<typename T> class Class1 { void error() { B::Class2 b; b.something_private = 42; } }; I orignally thought this to be a clang bug, it seems to actually be a problem in gcc incorrectly accepting this code. Richard Smith mentions in http://llvm.org/bugs/show_bug.cgi?id=17950: --8<-- 7.3.1.2/3: "If the name in a friend declaration is neither qualified nor a template-id and the declaration is a function or an elaborated-type-specifier, the lookup to determine whether the entity has been previously declared shall not consider any scopes outside the innermost enclosing namespace." Therefore a (not-yet-declared) 'template<typename V> class B::Class1;' is befriended, not the global Class1. -->8-- So gcc should require replacing the friend declaration by: template<typename V> friend class ::Class1;