# ostream.tcc

    00001 // ostream classes -*- C++ -*-
    00002
    00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
    00004 // Free Software Foundation, Inc.
    00005 //
    00006 // This file is part of the GNU ISO C++ Library.  This library is free
    00007 // software; you can redistribute it and/or modify it under the
    00008 // terms of the GNU General Public License as published by the
    00009 // Free Software Foundation; either version 2, or (at your option)
    00010 // any later version.
    00011
    00012 // This library is distributed in the hope that it will be useful,
    00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00015 // GNU General Public License for more details.
    00016
    00017 // You should have received a copy of the GNU General Public License along
    00018 // with this library; see the file COPYING.  If not, write to the Free
    00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    00020 // USA.
    00021
    00022 // As a special exception, you may use this file as part of a free software
    00023 // library without restriction.  Specifically, if other files instantiate
    00024 // templates or use macros or inline functions from this file, or you compile
    00025 // this file and link it with other files to produce an executable, this
    00026 // file does not by itself cause the resulting executable to be covered by
    00027 // the GNU General Public License.  This exception does not however
    00028 // invalidate any other reasons why the executable file might be covered by
    00029 // the GNU General Public License.
    00030
    00031 //
    00032 // ISO C++ 14882: 27.6.2  Output streams
    00033 //
    00034
    00035 #pragma GCC system_header
    00036
    00037 #include <locale>
    00038
    00039 namespace std
    00040 {
    00041   template<typename _CharT, typename _Traits>
    00042     basic_ostream<_CharT, _Traits>::sentry::
    00043     sentry(basic_ostream<_CharT,_Traits>& __os)
    00044     : _M_os(__os)
    00045     {
    00046       // XXX MT
    00047       if (__os.tie() && __os.good())
    00048     __os.tie()->flush();
    00049
    00050       if (__os.good())
    00051     _M_ok = true;
    00052       else
    00053     {
    00054       _M_ok = false;
    00055       __os.setstate(ios_base::failbit);
    00056     }
    00057     }
    00058
    00059   template<typename _CharT, typename _Traits>
    00060     basic_ostream<_CharT, _Traits>&
    00061     basic_ostream<_CharT, _Traits>::
    00062     operator<<(__ostream_type& (*__pf)(__ostream_type&))
    00063     {
    00064       sentry __cerb(*this);
    00065       if (__cerb)
    00066     {
    00067       try
    00068         { __pf(*this); }
    00069       catch(...)
    00070         {
    00071           // 27.6.2.5.1 Common requirements.
    00072           // Turn this on without causing an ios::failure to be thrown.
    00073           this->_M_setstate(ios_base::badbit);
    00074           if ((this->exceptions() & ios_base::badbit) != 0)
    00075         __throw_exception_again;
    00076         }
    00077     }
    00078       return *this;
    00079     }
    00080
    00081   template<typename _CharT, typename _Traits>
    00082     basic_ostream<_CharT, _Traits>&
    00083     basic_ostream<_CharT, _Traits>::
    00084     operator<<(__ios_type& (*__pf)(__ios_type&))
    00085     {
    00086       sentry __cerb(*this);
    00087       if (__cerb)
    00088     {
    00089       try
    00090         { __pf(*this); }
    00091       catch(...)
    00092         {
    00093           // 27.6.2.5.1 Common requirements.
    00094           // Turn this on without causing an ios::failure to be thrown.
    00095           this->_M_setstate(ios_base::badbit);
    00096           if ((this->exceptions() & ios_base::badbit) != 0)
    00097         __throw_exception_again;
    00098         }
    00099     }
    00100       return *this;
    00101     }
    00102
    00103   template<typename _CharT, typename _Traits>
    00104     basic_ostream<_CharT, _Traits>&
    00105     basic_ostream<_CharT, _Traits>::
    00106     operator<<(ios_base& (*__pf)(ios_base&))
    00107     {
    00108       sentry __cerb(*this);
    00109       if (__cerb)
    00110     {
    00111       try
    00112         { __pf(*this); }
    00113       catch(...)
    00114         {
    00115           // 27.6.2.5.1 Common requirements.
    00116           // Turn this on without causing an ios::failure to be thrown.
    00117           this->_M_setstate(ios_base::badbit);
    00118           if ((this->exceptions() & ios_base::badbit) != 0)
    00119         __throw_exception_again;
    00120         }
    00121     }
    00122       return *this;
    00123     }
    00124
    00125   template<typename _CharT, typename _Traits>
    00126     basic_ostream<_CharT, _Traits>&
    00127     basic_ostream<_CharT, _Traits>::operator<<(__streambuf_type* __sbin)
    00128     {
    00129       sentry __cerb(*this);
    00130       if (__cerb && __sbin)
    00131     {
    00132       try
    00133         {
    00134           if (!__copy_streambufs(*this, __sbin, this->rdbuf()))
    00135         this->setstate(ios_base::failbit);
    00136         }
    00137       catch(...)
    00138         {
    00139           // 27.6.2.5.1 Common requirements.
    00140           // Turn this on without causing an ios::failure to be thrown.
    00141           this->_M_setstate(ios_base::badbit);
    00142           if ((this->exceptions() & ios_base::badbit) != 0)
    00143         __throw_exception_again;
    00144         }
    00145     }
    00146       else if (!__sbin)
    00147     this->setstate(ios_base::badbit);
    00148       return *this;
    00149     }
    00150
    00151   template<typename _CharT, typename _Traits>
    00152     basic_ostream<_CharT, _Traits>&
    00153     basic_ostream<_CharT, _Traits>::operator<<(bool __n)
    00154     {
    00155       sentry __cerb(*this);
    00156       if (__cerb)
    00157     {
    00158       try
    00159         {
    00160           if (_M_check_facet(_M_fnumput))
    00161         if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
    00162           this->setstate(ios_base::badbit);
    00163         }
    00164       catch(...)
    00165         {
    00166           // 27.6.1.2.1 Common requirements.
    00167           // Turn this on without causing an ios::failure to be thrown.
    00168           this->_M_setstate(ios_base::badbit);
    00169           if ((this->exceptions() & ios_base::badbit) != 0)
    00170         __throw_exception_again;
    00171         }
    00172     }
    00173       return *this;
    00174     }
    00175
    00176   template<typename _CharT, typename _Traits>
    00177     basic_ostream<_CharT, _Traits>&
    00178     basic_ostream<_CharT, _Traits>::operator<<(long __n)
    00179     {
    00180       sentry __cerb(*this);
    00181       if (__cerb)
    00182     {
    00183       try
    00184         {
    00185           char_type __c = this->fill();
    00186           ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
    00187           if (_M_check_facet(_M_fnumput))
    00188         {
    00189           bool __b = false;
    00190           if ((__fmt & ios_base::oct) || (__fmt & ios_base::hex))
    00191             {
    00192               unsigned long __l = static_cast<unsigned long>(__n);
    00193               __b = _M_fnumput->put(*this, *this, __c, __l).failed();
    00194             }
    00195           else
    00196             __b = _M_fnumput->put(*this, *this, __c, __n).failed();
    00197           if (__b)
    00198             this->setstate(ios_base::badbit);
    00199         }
    00200         }
    00201       catch(...)
    00202         {
    00203           // 27.6.1.2.1 Common requirements.
    00204           // Turn this on without causing an ios::failure to be thrown.
    00205           this->_M_setstate(ios_base::badbit);
    00206           if ((this->exceptions() & ios_base::badbit) != 0)
    00207         __throw_exception_again;
    00208         }
    00209     }
    00210       return *this;
    00211     }
    00212
    00213   template<typename _CharT, typename _Traits>
    00214     basic_ostream<_CharT, _Traits>&
    00215     basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n)
    00216     {
    00217       sentry __cerb(*this);
    00218       if (__cerb)
    00219     {
    00220       try
    00221         {
    00222           if (_M_check_facet(_M_fnumput))
    00223         if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
    00224           this->setstate(ios_base::badbit);
    00225         }
    00226       catch(...)
    00227         {
    00228           // 27.6.1.2.1 Common requirements.
    00229           // Turn this on without causing an ios::failure to be thrown.
    00230           this->_M_setstate(ios_base::badbit);
    00231           if ((this->exceptions() & ios_base::badbit) != 0)
    00232         __throw_exception_again;
    00233         }
    00234     }
    00235       return *this;
    00236     }
    00237
    00238 #ifdef _GLIBCPP_USE_LONG_LONG
    00239   template<typename _CharT, typename _Traits>
    00240     basic_ostream<_CharT, _Traits>&
    00241     basic_ostream<_CharT, _Traits>::operator<<(long long __n)
    00242     {
    00243       sentry __cerb(*this);
    00244       if (__cerb)
    00245     {
    00246       try
    00247         {
    00248           char_type __c = this->fill();
    00249           ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
    00250           if (_M_check_facet(_M_fnumput))
    00251         {
    00252           bool __b = false;
    00253           if ((__fmt & ios_base::oct) || (__fmt & ios_base::hex))
    00254             {
    00255               unsigned long long __l;
    00256               __l = static_cast<unsigned long long>(__n);
    00257               __b = _M_fnumput->put(*this, *this, __c, __l).failed();
    00258             }
    00259           else
    00260             __b = _M_fnumput->put(*this, *this, __c, __n).failed();
    00261           if (__b)
    00262             this->setstate(ios_base::badbit);
    00263         }
    00264         }
    00265       catch(...)
    00266         {
    00267           // 27.6.1.2.1 Common requirements.
    00268           // Turn this on without causing an ios::failure to be thrown.
    00269           this->_M_setstate(ios_base::badbit);
    00270           if ((this->exceptions() & ios_base::badbit) != 0)
    00271         __throw_exception_again;
    00272         }
    00273     }
    00274       return *this;
    00275     }
    00276
    00277   template<typename _CharT, typename _Traits>
    00278     basic_ostream<_CharT, _Traits>&
    00279     basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n)
    00280     {
    00281       sentry __cerb(*this);
    00282       if (__cerb)
    00283     {
    00284       try
    00285         {
    00286           if (_M_check_facet(_M_fnumput))
    00287         if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
    00288           this->setstate(ios_base::badbit);
    00289         }
    00290       catch(...)
    00291         {
    00292           // 27.6.1.2.1 Common requirements.
    00293           // Turn this on without causing an ios::failure to be thrown.
    00294           this->_M_setstate(ios_base::badbit);
    00295           if ((this->exceptions() & ios_base::badbit) != 0)
    00296         __throw_exception_again;
    00297         }
    00298     }
    00299       return *this;
    00300     }
    00301 #endif
    00302
    00303   template<typename _CharT, typename _Traits>
    00304     basic_ostream<_CharT, _Traits>&
    00305     basic_ostream<_CharT, _Traits>::operator<<(double __n)
    00306     {
    00307       sentry __cerb(*this);
    00308       if (__cerb)
    00309     {
    00310       try
    00311         {
    00312           if (_M_check_facet(_M_fnumput))
    00313         if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
    00314           this->setstate(ios_base::badbit);
    00315         }
    00316       catch(...)
    00317         {
    00318           // 27.6.1.2.1 Common requirements.
    00319           // Turn this on without causing an ios::failure to be thrown.
    00320           this->_M_setstate(ios_base::badbit);
    00321           if ((this->exceptions() & ios_base::badbit) != 0)
    00322         __throw_exception_again;
    00323         }
    00324     }
    00325       return *this;
    00326     }
    00327
    00328   template<typename _CharT, typename _Traits>
    00329     basic_ostream<_CharT, _Traits>&
    00330     basic_ostream<_CharT, _Traits>::operator<<(long double __n)
    00331     {
    00332       sentry __cerb(*this);
    00333       if (__cerb)
    00334     {
    00335       try
    00336         {
    00337           if (_M_check_facet(_M_fnumput))
    00338         if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
    00339           this->setstate(ios_base::badbit);
    00340         }
    00341       catch(...)
    00342         {
    00343           // 27.6.1.2.1 Common requirements.
    00344           // Turn this on without causing an ios::failure to be thrown.
    00345           this->_M_setstate(ios_base::badbit);
    00346           if ((this->exceptions() & ios_base::badbit) != 0)
    00347         __throw_exception_again;
    00348         }
    00349     }
    00350       return *this;
    00351     }
    00352
    00353   template<typename _CharT, typename _Traits>
    00354     basic_ostream<_CharT, _Traits>&
    00355     basic_ostream<_CharT, _Traits>::operator<<(const void* __n)
    00356     {
    00357       sentry __cerb(*this);
    00358       if (__cerb)
    00359     {
    00360       try
    00361         {
    00362           if (_M_check_facet(_M_fnumput))
    00363         if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
    00364           this->setstate(ios_base::badbit);
    00365         }
    00366       catch(...)
    00367         {
    00368           // 27.6.1.2.1 Common requirements.
    00369           // Turn this on without causing an ios::failure to be thrown.
    00370           this->_M_setstate(ios_base::badbit);
    00371           if ((this->exceptions() & ios_base::badbit) != 0)
    00372         __throw_exception_again;
    00373         }
    00374     }
    00375       return *this;
    00376     }
    00377
    00378   template<typename _CharT, typename _Traits>
    00379     basic_ostream<_CharT, _Traits>&
    00380     basic_ostream<_CharT, _Traits>::put(char_type __c)
    00381     {
    00382       sentry __cerb(*this);
    00383       if (__cerb)
    00384     {
    00385       int_type __put = rdbuf()->sputc(__c);
    00386       if (traits_type::eq_int_type(__put, traits_type::eof()))
    00387         this->setstate(ios_base::badbit);
    00388     }
    00389       return *this;
    00390     }
    00391
    00392   template<typename _CharT, typename _Traits>
    00393     basic_ostream<_CharT, _Traits>&
    00394     basic_ostream<_CharT, _Traits>::write(const _CharT* __s, streamsize __n)
    00395     {
    00396       sentry __cerb(*this);
    00397       if (__cerb)
    00398     {
    00399       streamsize __put = this->rdbuf()->sputn(__s, __n);
    00400       if ( __put != __n)
    00401         this->setstate(ios_base::badbit);
    00402     }
    00403       return *this;
    00404     }
    00405
    00406   template<typename _CharT, typename _Traits>
    00407     basic_ostream<_CharT, _Traits>&
    00408     basic_ostream<_CharT, _Traits>::flush()
    00409     {
    00410       sentry __cerb(*this);
    00411       if (__cerb)
    00412     {
    00413       if (this->rdbuf() && this->rdbuf()->pubsync() == -1)
    00414         this->setstate(ios_base::badbit);
    00415     }
    00416       return *this;
    00417     }
    00418
    00419   template<typename _CharT, typename _Traits>
    00420     typename basic_ostream<_CharT, _Traits>::pos_type
    00421     basic_ostream<_CharT, _Traits>::tellp()
    00422     {
    00423       pos_type __ret = pos_type(-1);
    00424       if (!this->fail())
    00425     __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
    00426       return __ret;
    00427     }
    00428
    00429
    00430   template<typename _CharT, typename _Traits>
    00431     basic_ostream<_CharT, _Traits>&
    00432     basic_ostream<_CharT, _Traits>::seekp(pos_type __pos)
    00433     {
    00434       if (!this->fail())
    00435     {
    00436 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
    00437 // 136.  seekp, seekg setting wrong streams?
    00438       pos_type __err = this->rdbuf()->pubseekpos(__pos, ios_base::out);
    00439
    00440 // 129. Need error indication from seekp() and seekg()
    00441       if (__err == pos_type(off_type(-1)))
    00442         this->setstate(ios_base::failbit);
    00443 #endif
    00444     }
    00445       return *this;
    00446     }
    00447
    00448   template<typename _CharT, typename _Traits>
    00449     basic_ostream<_CharT, _Traits>&
    00450     basic_ostream<_CharT, _Traits>::
    00451     seekp(off_type __off, ios_base::seekdir __d)
    00452     {
    00453       if (!this->fail())
    00454     {
    00455 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
    00456 // 136.  seekp, seekg setting wrong streams?
    00457       pos_type __err = this->rdbuf()->pubseekoff(__off, __d,
    00458                              ios_base::out);
    00459
    00460 // 129. Need error indication from seekp() and seekg()
    00461       if (__err == pos_type(off_type(-1)))
    00462         this->setstate(ios_base::failbit);
    00463 #endif
    00464     }
    00465       return *this;
    00466     }
    00467
    00468   // 27.6.2.5.4 Character inserters.
    00469   template<typename _CharT, typename _Traits>
    00470     basic_ostream<_CharT, _Traits>&
    00471     operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
    00472     {
    00473       typedef basic_ostream<_CharT, _Traits> __ostream_type;
    00474       typename __ostream_type::sentry __cerb(__out);
    00475       if (__cerb)
    00476     {
    00477       try
    00478         {
    00479           streamsize __w = __out.width();
    00480           _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * (__w + 1)));
    00481           __pads[0] = __c;
    00482           streamsize __len = 1;
    00483           if (__w > __len)
    00484         {
    00485           __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __pads,
    00486                          &__c, __w, __len, false);
    00487           __len = __w;
    00488         }
    00489           __out.write(__pads, __len);
    00490           __out.width(0);
    00491         }
    00492       catch(...)
    00493         {
    00494           // 27.6.1.2.1 Common requirements.
    00495           // Turn this on without causing an ios::failure to be thrown.
    00496           __out._M_setstate(ios_base::badbit);
    00497           if ((__out.exceptions() & ios_base::badbit) != 0)
    00498         __throw_exception_again;
    00499         }
    00500     }
    00501       return __out;
    00502     }
    00503
    00504   // Specializations.
    00505   template <class _Traits>
    00506     basic_ostream<char, _Traits>&
    00507     operator<<(basic_ostream<char, _Traits>& __out, char __c)
    00508     {
    00509       typedef basic_ostream<char, _Traits> __ostream_type;
    00510       typename __ostream_type::sentry __cerb(__out);
    00511       if (__cerb)
    00512     {
    00513       try
    00514         {
    00515           streamsize __w = __out.width();
    00516           char* __pads = static_cast<char*>(__builtin_alloca(__w + 1));
    00517           __pads[0] = __c;
    00518           streamsize __len = 1;
    00519           if (__w > __len)
    00520         {
    00521           __pad<char, _Traits>::_S_pad(__out, __out.fill(), __pads,
    00522                            &__c, __w, __len, false);
    00523           __len = __w;
    00524         }
    00525           __out.write(__pads, __len);
    00526           __out.width(0);
    00527         }
    00528       catch(...)
    00529         {
    00530           // 27.6.1.2.1 Common requirements.
    00531           // Turn this on without causing an ios::failure to be thrown.
    00532           __out._M_setstate(ios_base::badbit);
    00533           if ((__out.exceptions() & ios_base::badbit) != 0)
    00534         __throw_exception_again;
    00535         }
    00536     }
    00537       return __out;
    00538      }
    00539
    00540   template<typename _CharT, typename _Traits>
    00541     basic_ostream<_CharT, _Traits>&
    00542     operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
    00543     {
    00544       typedef basic_ostream<_CharT, _Traits> __ostream_type;
    00545       typename __ostream_type::sentry __cerb(__out);
    00546       if (__cerb && __s)
    00547     {
    00548       try
    00549         {
    00550           streamsize __w = __out.width();
    00551           _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
    00552           streamsize __len = static_cast<streamsize>(_Traits::length(__s));
    00553           if (__w > __len)
    00554         {
    00555           __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __pads,
    00556                          __s, __w, __len, false);
    00557           __s = __pads;
    00558           __len = __w;
    00559         }
    00560           __out.write(__s, __len);
    00561           __out.width(0);
    00562         }
    00563       catch(...)
    00564         {
    00565           // 27.6.1.2.1 Common requirements.
    00566           // Turn this on without causing an ios::failure to be thrown.
    00567           __out._M_setstate(ios_base::badbit);
    00568           if ((__out.exceptions() & ios_base::badbit) != 0)
    00569         __throw_exception_again;
    00570         }
    00571     }
    00572       else if (!__s)
    00573     __out.setstate(ios_base::badbit);
    00574       return __out;
    00575     }
    00576
    00577   template<typename _CharT, typename _Traits>
    00578     basic_ostream<_CharT, _Traits>&
    00579     operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
    00580     {
    00581       typedef basic_ostream<_CharT, _Traits> __ostream_type;
    00582 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
    00583 // 167.  Improper use of traits_type::length()
    00584 // Note that this is only in 'Review' status.
    00585       typedef char_traits<char>          __traits_type;
    00586 #endif
    00587       typename __ostream_type::sentry __cerb(__out);
    00588       if (__cerb && __s)
    00589     {
    00590       size_t __clen = __traits_type::length(__s);
    00591       _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * (__clen + 1)));
    00592       for (size_t  __i = 0; __i < __clen; ++__i)
    00593         __ws[__i] = __out.widen(__s[__i]);
    00594       _CharT* __str = __ws;
    00595
    00596       try
    00597         {
    00598           streamsize __len = static_cast<streamsize>(__clen);
    00599           streamsize __w = __out.width();
    00600           _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
    00601
    00602           if (__w > __len)
    00603         {
    00604           __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __pads,
    00605                          __ws, __w, __len, false);
    00606           __str = __pads;
    00607           __len = __w;
    00608         }
    00609           __out.write(__str, __len);
    00610           __out.width(0);
    00611         }
    00612       catch(...)
    00613         {
    00614           // 27.6.1.2.1 Common requirements.
    00615           // Turn this on without causing an ios::failure to be thrown.
    00616           __out._M_setstate(ios_base::badbit);
    00617           if ((__out.exceptions() & ios_base::badbit) != 0)
    00618         __throw_exception_again;
    00619         }
    00620     }
    00621       else if (!__s)
    00622     __out.setstate(ios_base::badbit);
    00623       return __out;
    00624     }
    00625
    00626   // Partial specializations.
    00627   template<class _Traits>
    00628     basic_ostream<char, _Traits>&
    00629     operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
    00630     {
    00631       typedef basic_ostream<char, _Traits> __ostream_type;
    00632       typename __ostream_type::sentry __cerb(__out);
    00633       if (__cerb && __s)
    00634     {
    00635       try
    00636         {
    00637           streamsize __w = __out.width();
    00638           char* __pads = static_cast<char*>(__builtin_alloca(__w));
    00639           streamsize __len = static_cast<streamsize>(_Traits::length(__s));
    00640
    00641           if (__w > __len)
    00642         {
    00643           __pad<char, _Traits>::_S_pad(__out, __out.fill(), __pads,
    00644                          __s, __w, __len, false);
    00645           __s = __pads;
    00646           __len = __w;
    00647         }
    00648           __out.write(__s, __len);
    00649           __out.width(0);
    00650         }
    00651       catch(...)
    00652         {
    00653           // 27.6.1.2.1 Common requirements.
    00654           // Turn this on without causing an ios::failure to be thrown.
    00655           __out._M_setstate(ios_base::badbit);
    00656           if ((__out.exceptions() & ios_base::badbit) != 0)
    00657         __throw_exception_again;
    00658         }
    00659     }
    00660       else if (!__s)
    00661     __out.setstate(ios_base::badbit);
    00662       return __out;
    00663     }
    00664
    00665   // 21.3.7.9 basic_string::operator<<
    00666   template<typename _CharT, typename _Traits, typename _Alloc>
    00667     basic_ostream<_CharT, _Traits>&
    00668     operator<<(basic_ostream<_CharT, _Traits>& __out,
    00669            const basic_string<_CharT, _Traits, _Alloc>& __str)
    00670     {
    00671       typedef basic_ostream<_CharT, _Traits> __ostream_type;
    00672       typename __ostream_type::sentry __cerb(__out);
    00673       if (__cerb)
    00674     {
    00675       const _CharT* __s = __str.data();
    00676       streamsize __w = __out.width();
    00677       _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
    00678       streamsize __len = static_cast<streamsize>(__str.size());
    00679 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
    00680       // 25. String operator<< uses width() value wrong
    00681 #endif
    00682       if (__w > __len)
    00683         {
    00684           __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __pads, __s,
    00685                          __w, __len, false);
    00686           __s = __pads;
    00687           __len = __w;
    00688         }
    00689       streamsize __res = __out.rdbuf()->sputn(__s, __len);
    00690       __out.width(0);
    00691       if (__res != __len)
    00692         __out.setstate(ios_base::failbit);
    00693     }
    00694       return __out;
    00695     }
    00696
    00697   // Inhibit implicit instantiations for required instantiations,
    00698   // which are defined via explicit instantiations elsewhere.
    00699   // NB:  This syntax is a GNU extension.
    00700 #if _GLIBCPP_EXTERN_TEMPLATE
    00701   extern template class basic_ostream<char>;
    00702   extern template ostream& endl(ostream&);
    00703   extern template ostream& ends(ostream&);
    00704   extern template ostream& flush(ostream&);
    00705   extern template ostream& operator<<(ostream&, char);
    00706   extern template ostream& operator<<(ostream&, unsigned char);
    00707   extern template ostream& operator<<(ostream&, signed char);
    00708   extern template ostream& operator<<(ostream&, const char*);
    00709   extern template ostream& operator<<(ostream&, const unsigned char*);
    00710   extern template ostream& operator<<(ostream&, const signed char*);
    00711
    00712 #ifdef _GLIBCPP_USE_WCHAR_T
    00713   extern template class basic_ostream<wchar_t>;
    00714   extern template wostream& endl(wostream&);
    00715   extern template wostream& ends(wostream&);
    00716   extern template wostream& flush(wostream&);
    00717   extern template wostream& operator<<(wostream&, wchar_t);
    00718   extern template wostream& operator<<(wostream&, char);
    00719   extern template wostream& operator<<(wostream&, const wchar_t*);
    00720   extern template wostream& operator<<(wostream&, const char*);
    00721 #endif
    00722 #endif
    00723 } // namespace std