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