Hi, I tried to compile quite simple code: a template and operator<<(ostreami&) for it (as a template function). The template is in a namespace, but operator<< I'd like to have in the 'std' namespace.
Unfortunately, the compiler rejected the code, however other compilers (i.e. in other versions) compiled the code without any complainits. I'm quite sure that the code is correct, buti, to be honest, I didn't check what c++ iso standard is saying about it. I tested the following code on my on version 3.4.3-20050101 and then build and tested on the 3.4.4-20050506 snapshot. The issue occured in both versions. On the other hand, I compiled successfully the same code on g++ 3.3.5-20050130 that is installed on other (old&slow) box (gentoo, i586, for details see below). Additionally, I tested the same code on 2.95.x on Solaris (SunOS 5.9? - but I don't remember right now) - it was also compiled. I didn't check gcc-4.x. Summary: The code (problem.cc): #include<iostream> using namespace std; namespace a { template<typename T> class A; } template<typename T> std::ostream & std::operator<< (std::ostream &, a::A<T> const &); namespace a { template<typename T> class A { T v; public: A(T _v) : v(_v) {}; friend std::ostream & std::operator<< <>(std::ostream &, A<T> const &); }; } template<typename T> std::ostream & std::operator<<(std::ostream & o, a::A<T> const & aa) { return o << aa.v ; } int main() { a::A<float> o(16.0); cerr << o << endl; } Command: g++ problem.cc Output: problem.cc: In instantiation of `a::A<float>': problem.cc:32: instantiated from here problem.cc:19: error: template-id `operator<< <>' for `std::basic_ostream<char, std::char_traits<char> >& std::operator<<(std::basic_ostream<char, std::char_traits<char> >&, const a::A<float>&)' does not match any template declaration problem.cc: In function `std::ostream& std::operator<<(std::ostream&, const a::A<T>&) [with T = float]': problem.cc:34: instantiated from here problem.cc:16: error: `float a::A<float>::v' is private problem.cc:27: error: within this context Compilation failed on 3.4.3-20050101 and 3.4.4-20050506 on my box: Release: 3.4.4 20050506 (prerelease) Environment: System: Linux baki 2.6.12-rc1baki #5 Sat Mar 26 09:01:44 UTC 2005 i686 AMD Athlon(tm) XP 2000+ AuthenticAMD GNU/Linux Architecture: i686 host: i686-pc-linux-gnu build: i686-pc-linux-gnu target: i686-pc-linux-gnu configured with: : (reconfigured) ./configure --prefix=/home/marciso/gcc-tmp-installatio --enable-languages=c++ Release:3.4.3-20050110 (Gentoo Linux 3.4.3.20050110-r2, ssp-3.4.3.20050110-0, pie-8.7.7) Environment: System: Linux baki 2.6.12-rc1baki #5 Sat Mar 26 09:01:44 UTC 2005 i686 AMD Athlon(tm) XP 2000+ AuthenticAMD GNU/Linux Architecture: i686 host: i686-pc-linux-gnu build: i686-pc-linux-gnu target: i686-pc-linux-gnu configured with: /var/tmp/portage/gcc-3.4.3.20050110-r2/work/gcc-3.4.3/configure --enable-version-specific-runtime-libs --prefix=/usr --bindir=/usr/i686-pc-linux-gnu/gcc-bin/3.4.3-20050110 --includedir=/usr/lib/gcc/i686-pc-linux-gnu/3.4.3-20050110/include --datadir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4.3-20050110 --mandir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4.3-20050110/man --infodir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4.3-20050110/info --with-gxx-include-dir=/usr/lib/gcc/i686-pc-linux-gnu/3.4.3-20050110/include/g++-v3 --host=i686-pc-linux-gnu --disable-altivec --enable-nls --without-included-gettext --with-system-zlib --disable-checking --disable-werror --disable-libunwind-exceptions --disable-multilib --enable-java-awt=gtk --enable-languages=c,c++,objc,java,f77 --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu Compilation passed on 3.3.5-20050130: Release: 3.3.5-20050130 (Gentoo Linux 3.3.5.20050130-r1, ssp-3.3.5.20050130-1, pie-8.7.7.1) Environment: System: Linux gateway 2.6.10-hardened-r3bramka #2 Sun Feb 20 12:58:22 CET 2005 i586 GNU/Linux Architecture: i586 host: i586-pc-linux-gnu build: i586-pc-linux-gnu target: i586-pc-linux-gnu configured with: /var/tmp/portage/gcc-3.3.5.20050130-r1/work/gcc-3.3.5/configure --enable-version-specific-runtime-libs --prefix=/usr --bindir=/usr/i586-pc-linux-gnu/gcc-bin/3.3.5-20050130 --includedir=/usr/lib/gcc-lib/i586-pc-linux-gnu/3.3.5-20050130/include --datadir=/usr/share/gcc-data/i586-pc-linux-gnu/3.3.5-20050130 --mandir=/usr/share/gcc-data/i586-pc-linux-gnu/3.3.5-20050130/man --infodir=/usr/share/gcc-data/i586-pc-linux-gnu/3.3.5-20050130/info --with-gxx-include-dir=/usr/lib/gcc-lib/i586-pc-linux-gnu/3.3.5-20050130/include/g++-v3 --host=i586-pc-linux-gnu --disable-altivec --enable-nls --without-included-gettext --with-system-zlib --disable-checking --disable-werror --disable-libunwind-exceptions --disable-multilib --disable-libgcj --enable-languages=c,c++,f77 --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu Cheers Marcin. -- Summary: problem with tempalte and tempalte function compilation in a namespace Product: gcc Version: 3.3.4 Status: UNCONFIRMED Severity: normal Priority: P2 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: marciso at box43 dot pl CC: gcc-bugs at gcc dot gnu dot org GCC build triplet: i686-pc-linux-gnu GCC host triplet: i686-pc-linux-gnu GCC target triplet: i686-pc-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21515