------- Comment #7 from yimingli0126 at 163 dot com 2009-09-02 10:33 ------- Thanks.
I have added explicit copy constructors both Inner(Inner const&) and Inner(Inner&) to the class Inner, which are superior to template < class IStream > Inner (IStream&) when the complier decides which should be called, so that now the codes work well. Here are the new sample codes: struct Inner { template < class IStream > friend IStream& operator >> ( IStream& in, Inner& inner ); Inner() : data(0) {} Inner( Inner const& inner ) : data(inner.data) {} // Make the class copy-constructable. Inner( Inner& ); // Only declaration is necessary fo gcc. template < class IStream > explicit Inner( IStream& in ) { operator >> (in,*this); } int data; }; template < class IStream > IStream& operator >> ( IStream& in, Inner& inner ) { in >> inner.data; return in; } struct Outter { template < class IStream > friend IStream& operator >> ( IStream& in, Outter& outter ); Outter() {} Outter( Outter const& outter) {} // Make the class copy-constructable. Outter( Outter& ); // Only declaration is necessary. template < class IStream > explicit Outter( IStream& in ) { operator >> (in,*this); } std::vector<Inner> inners; }; template < class IStream > IStream& operator >> ( IStream& in, Outter& outter ) { outter.inners.push_back(Inner(in)); return in; } int main ( int argc, char* argv[] ) { Inner inner(std::cin); Outter outter(std::cin); return 0; } Actually, becuase there is a delcaration of Inner(Inner&), the instance of template < class IStream > Outter( IStream& ) with IStream = Inner is suppressed. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41216