http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57713
Bug ID: 57713 Summary: Template functions see friendship as inherited Product: gcc Version: 4.8.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: w.shane.grant at gmail dot com Template functions that are friends of a class bypass access control for derived classes. ---------- template <class T> T&& declval(); template <class T> auto foo() -> decltype(declval<T&>().bar()) {} class Base { private: template <class T> friend auto foo() -> decltype(declval<T&>().bar()); void bar(){} }; class Derived : public Base {}; int main() { foo<Derived>(); } ----------- bar should be private in Derived but the friend declaration in Base is allowing foo to access it, preventing compilation from failing. Here's another very similar example: --------- template <class T> void foo(T&&t) { t.bar(); } class Base { private: template <class T> friend void foo(T&&); void bar(){} }; class Derived : public Base {}; int main() { foo(Derived()); } -------- Derived gets the ability to call bar() only inside of the template function foo. Happens on (4.9.0 20130625 (experimental)) and (4.8.1).