http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50126
Bug #: 50126 Summary: g++ does not recognize a friend declaration Classification: Unclassified Product: gcc Version: 4.4.5 Status: UNCONFIRMED Severity: minor Priority: P3 Component: c++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: pisetta.gia...@alice.it Created attachment 25054 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=25054 intermediate file compiling the code of test.cpp: ----------------------------------- template<class A,class B> class X { public: template<class C> X<C,B>& operator[]( const C& ); }; template<class A,class B,class C> class Y : public X<C,B> { friend X<C,B>& X<A,B>::template operator[]<C>( const C& ); private: Y( X<A,B>& object , const C& index ) : X<C,B>() {}; }; template<class A,class B> template<class C> X<C,B>& X<A,B>::operator[]( const C& index ) { return *( new Y<A,B,C>( *this , index ) ); } void test() { X<int,void> x; X<int,void>& y = x[2]; } --------------------------------------- with the command line: g++ -c test.cpp -o test.cpp report: ./test.cpp: In instantiation of ‘Y<int, void, int>’: ./test.cpp:14: instantiated from ‘X<C, B>& X<A, B>::operator[](const C&) with [C = int, A = int, B = void]’ ./test.cpp:19: instantiated from here ./test.cpp:8: error: ‘operator[]’ not defined ./test.cpp: In member function ‘X<C, B>& X<A, B>::operator[](const C&) [with C = int, A = int, B = void]’: ./test.cpp:19: instantiated from here ./test.cpp:10: error: ‘Y<A, B, C>::Y(X<A, B>&, const C&) [with A = int, B = void, C = int]’ is private ./test.cpp:14: error: within this context I asked on the internet and it seems that it is a valid friend declaration. Compiling the same code with 'clang' does not report any error. The only workaround is to use a template friend declaration like: -------------------------------------------- template<class A,class B,class C> class Y : public X<C,B> { template<class CC> friend X<CC,B>& X<A,B>::operator[]( const CC& ); -------------------------------------------- but it declares every generated operator[] as friend of class Y, so it is dangerous. g++ version: Using built-in specs. Target: i486-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.5-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-targets=all --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu Thread model: posix gcc version 4.4.5 (Debian 4.4.5-8)