When a friend function is defined in inside a class, and then passed to a STL algorithm function, gcc 4.1 reports it can't find the friend function.
gcc 4.0 and gcc 3.4.4 will compile the following code snippet. gcc 4.1.0 (Fedora 5 x86-64) won't compile, it returns this error message. friend_gcc4_bug.cc: In function int main(int, char**): friend_gcc4_bug.cc:32: error: Compare was not declared in this scope But gcc 4.1.0 will compile it if the friend is defined outside the class, rather than inside. friend_gcc4_bug.cc: ------------------- #include <iostream> #include <vector> #include <algorithm> #define FRIEND_IN_CLASS 1 class Class { private: int mVal; #if FRIEND_IN_CLASS friend bool Compare( const Class& a, const Class& b ) { return a.mVal < b.mVal; } #else friend bool Compare( const Class& a, const Class& b ); #endif }; #if ! FRIEND_IN_CLASS bool Compare( const Class& a, const Class& b ) { return a.mVal < b.mVal; } #endif int main( int argc, char** argv ) { std::vector<Class> vec; std::sort( vec.begin(), vec.end(), Compare ); std::nth_element( vec.begin(), vec.begin(), vec.end(), Compare ); std::search( vec.begin(), vec.end(), vec.begin(), vec.end(), Compare ); Class a, b; Compare( a, b ); } -- Summary: friend function defined inside class declaration Product: gcc Version: 4.1.0 Status: UNCONFIRMED Severity: minor Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: nathanbates99 at yahoo dot com GCC build triplet: x86_64-redhat-linux GCC host triplet: x86_64-redhat-linux GCC target triplet: x86_64-redhat-linux http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28597