I can see the following implementation in <complex>: /// Insertion operator for complex values. template<typename _Tp, typename _CharT, class _Traits> basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) { basic_ostringstream<_CharT, _Traits> __s; __s.flags(__os.flags()); __s.imbue(__os.getloc()); __s.precision(__os.precision()); __s << '(' << __x.real() << ',' << __x.imag() << ')'; // This can fail! return __os << __s.str(); // And then this may be empty! } This implementation may cause the following test case to fail: #include <cstdlib> #include <cassert> #include <iostream> #include <complex> int main () { float const a_f ((01)); ::std:: complex < float > a_c ((a_f)); std:: stringstream a_s; assert (!(a_s << a_c) || ((a_c = float ()) == float () && a_s >> a_c && a_c == a_f)); return +EXIT_SUCCESS; } I cannot reproduce the failure because I do not know how to inject a memory allocation failure. However, I imagine it is possible. I suggest that the implementation be modified: /// Insertion operator for complex values. template<typename _Tp, typename _CharT, class _Traits> basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) { basic_ostringstream<_CharT, _Traits> __s; __s.flags(__os.flags()); __s.imbue(__os.getloc()); __s.precision(__os.precision()); __s << '(' << __x.real() << ',' << __x.imag() << ')'; __os. setstate(__s.rdstate()) ; return __os << __s.str(); } Thank you for your consideration.
The Standard is very precise here, and nothing changed for C++11. It says, literally: Effects: inserts the complex number x onto the stream o as if it were implemented as follows: template<class T, class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& o, const complex<T>& x) { basic_ostringstream<charT, traits> s; s.flags(o.flags()); s.imbue(o.getloc()); s.precision(o.precision()); s << ’(’ << x.real() << "," << x.imag() << ’)’; return o << s.str(); } thus, I don't see how we could to something else, like what you are suggesting, and remain conforming.
What failure scenario are you talking about? Surely if there's a memory allocation failure it will throw a bad_alloc exception, so __s.str() will never get called?