Reland "[clang-repl] Re-implement clang-interpreter as a test case."
[llvm-project.git] / libcxx / include / ostream
blobefeaee253eb9791c1d35b611c346f187d57b7e9b
1 // -*- C++ -*-
2 //===-------------------------- ostream -----------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_OSTREAM
11 #define _LIBCPP_OSTREAM
14     ostream synopsis
16 template <class charT, class traits = char_traits<charT> >
17 class basic_ostream
18     : virtual public basic_ios<charT,traits>
20 public:
21     // types (inherited from basic_ios (27.5.4)):
22     typedef charT                          char_type;
23     typedef traits                         traits_type;
24     typedef typename traits_type::int_type int_type;
25     typedef typename traits_type::pos_type pos_type;
26     typedef typename traits_type::off_type off_type;
28     // 27.7.2.2 Constructor/destructor:
29     explicit basic_ostream(basic_streambuf<char_type,traits>* sb);
30     basic_ostream(basic_ostream&& rhs);
31     virtual ~basic_ostream();
33     // 27.7.2.3 Assign/swap
34     basic_ostream& operator=(const basic_ostream& rhs) = delete; // C++14
35     basic_ostream& operator=(basic_ostream&& rhs);
36     void swap(basic_ostream& rhs);
38     // 27.7.2.4 Prefix/suffix:
39     class sentry;
41     // 27.7.2.6 Formatted output:
42     basic_ostream& operator<<(basic_ostream& (*pf)(basic_ostream&));
43     basic_ostream& operator<<(basic_ios<charT, traits>& (*pf)(basic_ios<charT,traits>&));
44     basic_ostream& operator<<(ios_base& (*pf)(ios_base&));
45     basic_ostream& operator<<(bool n);
46     basic_ostream& operator<<(short n);
47     basic_ostream& operator<<(unsigned short n);
48     basic_ostream& operator<<(int n);
49     basic_ostream& operator<<(unsigned int n);
50     basic_ostream& operator<<(long n);
51     basic_ostream& operator<<(unsigned long n);
52     basic_ostream& operator<<(long long n);
53     basic_ostream& operator<<(unsigned long long n);
54     basic_ostream& operator<<(float f);
55     basic_ostream& operator<<(double f);
56     basic_ostream& operator<<(long double f);
57     basic_ostream& operator<<(const void* p);
58     basic_ostream& operator<<(basic_streambuf<char_type,traits>* sb);
59     basic_ostream& operator<<(nullptr_t);
61     // 27.7.2.7 Unformatted output:
62     basic_ostream& put(char_type c);
63     basic_ostream& write(const char_type* s, streamsize n);
64     basic_ostream& flush();
66     // 27.7.2.5 seeks:
67     pos_type tellp();
68     basic_ostream& seekp(pos_type);
69     basic_ostream& seekp(off_type, ios_base::seekdir);
70 protected:
71     basic_ostream(const basic_ostream& rhs) = delete;
72     basic_ostream(basic_ostream&& rhs);
73     // 27.7.3.3 Assign/swap
74     basic_ostream& operator=(basic_ostream& rhs) = delete;
75     basic_ostream& operator=(const basic_ostream&& rhs);
76     void swap(basic_ostream& rhs);
79 // 27.7.2.6.4 character inserters
81 template<class charT, class traits>
82   basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, charT);
84 template<class charT, class traits>
85   basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, char);
87 template<class traits>
88   basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, char);
90 // signed and unsigned
92 template<class traits>
93   basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, signed char);
95 template<class traits>
96   basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, unsigned char);
98 // NTBS
99 template<class charT, class traits>
100   basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, const charT*);
102 template<class charT, class traits>
103   basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, const char*);
105 template<class traits>
106   basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, const char*);
108 // signed and unsigned
109 template<class traits>
110 basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, const signed char*);
112 template<class traits>
113   basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, const unsigned char*);
115 // swap:
116 template <class charT, class traits>
117   void swap(basic_ostream<charT, traits>& x, basic_ostream<charT, traits>& y);
119 template <class charT, class traits>
120   basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);
122 template <class charT, class traits>
123   basic_ostream<charT,traits>& ends(basic_ostream<charT,traits>& os);
125 template <class charT, class traits>
126   basic_ostream<charT,traits>& flush(basic_ostream<charT,traits>& os);
128 // rvalue stream insertion
129 template <class Stream, class T>
130   Stream&& operator<<(Stream&& os, const T& x);
132 }  // std
136 #include <__config>
137 #include <bitset>
138 #include <ios>
139 #include <iterator>
140 #include <locale>
141 #include <streambuf>
142 #include <version>
144 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
145 #pragma GCC system_header
146 #endif
148 _LIBCPP_BEGIN_NAMESPACE_STD
150 template <class _CharT, class _Traits>
151 class _LIBCPP_TEMPLATE_VIS basic_ostream
152     : virtual public basic_ios<_CharT, _Traits>
154 public:
155     // types (inherited from basic_ios (27.5.4)):
156     typedef _CharT                         char_type;
157     typedef _Traits                        traits_type;
158     typedef typename traits_type::int_type int_type;
159     typedef typename traits_type::pos_type pos_type;
160     typedef typename traits_type::off_type off_type;
162     // 27.7.2.2 Constructor/destructor:
163     inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
164     explicit basic_ostream(basic_streambuf<char_type, traits_type>* __sb)
165     { this->init(__sb); }
166     virtual ~basic_ostream();
167 protected:
168     inline _LIBCPP_INLINE_VISIBILITY
169     basic_ostream(basic_ostream&& __rhs);
171     // 27.7.2.3 Assign/swap
172     inline _LIBCPP_INLINE_VISIBILITY
173     basic_ostream& operator=(basic_ostream&& __rhs);
175     inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
176     void swap(basic_ostream& __rhs)
177     { basic_ios<char_type, traits_type>::swap(__rhs); }
179     basic_ostream           (const basic_ostream& __rhs) = delete;
180     basic_ostream& operator=(const basic_ostream& __rhs) = delete;
182 public:
183     // 27.7.2.4 Prefix/suffix:
184     class _LIBCPP_TEMPLATE_VIS sentry;
186     // 27.7.2.6 Formatted output:
187     inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
188     basic_ostream& operator<<(basic_ostream& (*__pf)(basic_ostream&))
189     { return __pf(*this); }
191     inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
192     basic_ostream& operator<<(basic_ios<char_type, traits_type>&
193                               (*__pf)(basic_ios<char_type,traits_type>&))
194     { __pf(*this); return *this; }
196     inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
197     basic_ostream& operator<<(ios_base& (*__pf)(ios_base&))
198     { __pf(*this); return *this; }
200     basic_ostream& operator<<(bool __n);
201     basic_ostream& operator<<(short __n);
202     basic_ostream& operator<<(unsigned short __n);
203     basic_ostream& operator<<(int __n);
204     basic_ostream& operator<<(unsigned int __n);
205     basic_ostream& operator<<(long __n);
206     basic_ostream& operator<<(unsigned long __n);
207     basic_ostream& operator<<(long long __n);
208     basic_ostream& operator<<(unsigned long long __n);
209     basic_ostream& operator<<(float __f);
210     basic_ostream& operator<<(double __f);
211     basic_ostream& operator<<(long double __f);
212     basic_ostream& operator<<(const void* __p);
213     basic_ostream& operator<<(basic_streambuf<char_type, traits_type>* __sb);
215     _LIBCPP_INLINE_VISIBILITY
216     basic_ostream& operator<<(nullptr_t)
217     { return *this << "nullptr"; }
219     // 27.7.2.7 Unformatted output:
220     basic_ostream& put(char_type __c);
221     basic_ostream& write(const char_type* __s, streamsize __n);
222     basic_ostream& flush();
224     // 27.7.2.5 seeks:
225     inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
226     pos_type tellp();
227     inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
228     basic_ostream& seekp(pos_type __pos);
229     inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
230     basic_ostream& seekp(off_type __off, ios_base::seekdir __dir);
232 protected:
233     _LIBCPP_INLINE_VISIBILITY
234     basic_ostream() {}  // extension, intentially does not initialize
237 template <class _CharT, class _Traits>
238 class _LIBCPP_TEMPLATE_VIS basic_ostream<_CharT, _Traits>::sentry
240     bool __ok_;
241     basic_ostream<_CharT, _Traits>& __os_;
243     sentry(const sentry&); // = delete;
244     sentry& operator=(const sentry&); // = delete;
246 public:
247     explicit sentry(basic_ostream<_CharT, _Traits>& __os);
248     ~sentry();
250     _LIBCPP_INLINE_VISIBILITY
251     explicit operator bool() const {return __ok_;}
254 template <class _CharT, class _Traits>
255 basic_ostream<_CharT, _Traits>::sentry::sentry(basic_ostream<_CharT, _Traits>& __os)
256     : __ok_(false),
257       __os_(__os)
259     if (__os.good())
260     {
261         if (__os.tie())
262             __os.tie()->flush();
263         __ok_ = true;
264     }
267 template <class _CharT, class _Traits>
268 basic_ostream<_CharT, _Traits>::sentry::~sentry()
270     if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf)
271                       && !uncaught_exception())
272     {
273 #ifndef _LIBCPP_NO_EXCEPTIONS
274         try
275         {
276 #endif // _LIBCPP_NO_EXCEPTIONS
277             if (__os_.rdbuf()->pubsync() == -1)
278                 __os_.setstate(ios_base::badbit);
279 #ifndef _LIBCPP_NO_EXCEPTIONS
280         }
281         catch (...)
282         {
283         }
284 #endif // _LIBCPP_NO_EXCEPTIONS
285     }
288 template <class _CharT, class _Traits>
289 basic_ostream<_CharT, _Traits>::basic_ostream(basic_ostream&& __rhs)
291     this->move(__rhs);
294 template <class _CharT, class _Traits>
295 basic_ostream<_CharT, _Traits>&
296 basic_ostream<_CharT, _Traits>::operator=(basic_ostream&& __rhs)
298     swap(__rhs);
299     return *this;
302 template <class _CharT, class _Traits>
303 basic_ostream<_CharT, _Traits>::~basic_ostream()
307 template <class _CharT, class _Traits>
308 basic_ostream<_CharT, _Traits>&
309 basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<char_type, traits_type>* __sb)
311 #ifndef _LIBCPP_NO_EXCEPTIONS
312     try
313     {
314 #endif // _LIBCPP_NO_EXCEPTIONS
315         sentry __s(*this);
316         if (__s)
317         {
318             if (__sb)
319             {
320 #ifndef _LIBCPP_NO_EXCEPTIONS
321                 try
322                 {
323 #endif // _LIBCPP_NO_EXCEPTIONS
324                     typedef istreambuf_iterator<_CharT, _Traits> _Ip;
325                     typedef ostreambuf_iterator<_CharT, _Traits> _Op;
326                     _Ip __i(__sb);
327                     _Ip __eof;
328                     _Op __o(*this);
329                     size_t __c = 0;
330                     for (; __i != __eof; ++__i, ++__o, ++__c)
331                     {
332                         *__o = *__i;
333                         if (__o.failed())
334                             break;
335                     }
336                     if (__c == 0)
337                         this->setstate(ios_base::failbit);
338 #ifndef _LIBCPP_NO_EXCEPTIONS
339                 }
340                 catch (...)
341                 {
342                     this->__set_failbit_and_consider_rethrow();
343                 }
344 #endif // _LIBCPP_NO_EXCEPTIONS
345             }
346             else
347                 this->setstate(ios_base::badbit);
348         }
349 #ifndef _LIBCPP_NO_EXCEPTIONS
350     }
351     catch (...)
352     {
353         this->__set_badbit_and_consider_rethrow();
354     }
355 #endif // _LIBCPP_NO_EXCEPTIONS
356     return *this;
359 template <class _CharT, class _Traits>
360 basic_ostream<_CharT, _Traits>&
361 basic_ostream<_CharT, _Traits>::operator<<(bool __n)
363 #ifndef _LIBCPP_NO_EXCEPTIONS
364     try
365     {
366 #endif // _LIBCPP_NO_EXCEPTIONS
367         sentry __s(*this);
368         if (__s)
369         {
370             typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
371             const _Fp& __f = use_facet<_Fp>(this->getloc());
372             if (__f.put(*this, *this, this->fill(), __n).failed())
373                 this->setstate(ios_base::badbit | ios_base::failbit);
374         }
375 #ifndef _LIBCPP_NO_EXCEPTIONS
376     }
377     catch (...)
378     {
379         this->__set_badbit_and_consider_rethrow();
380     }
381 #endif // _LIBCPP_NO_EXCEPTIONS
382     return *this;
385 template <class _CharT, class _Traits>
386 basic_ostream<_CharT, _Traits>&
387 basic_ostream<_CharT, _Traits>::operator<<(short __n)
389 #ifndef _LIBCPP_NO_EXCEPTIONS
390     try
391     {
392 #endif // _LIBCPP_NO_EXCEPTIONS
393         sentry __s(*this);
394         if (__s)
395         {
396             ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
397             typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
398             const _Fp& __f = use_facet<_Fp>(this->getloc());
399             if (__f.put(*this, *this, this->fill(),
400                         __flags == ios_base::oct || __flags == ios_base::hex ?
401                         static_cast<long>(static_cast<unsigned short>(__n))  :
402                         static_cast<long>(__n)).failed())
403                 this->setstate(ios_base::badbit | ios_base::failbit);
404         }
405 #ifndef _LIBCPP_NO_EXCEPTIONS
406     }
407     catch (...)
408     {
409         this->__set_badbit_and_consider_rethrow();
410     }
411 #endif // _LIBCPP_NO_EXCEPTIONS
412     return *this;
415 template <class _CharT, class _Traits>
416 basic_ostream<_CharT, _Traits>&
417 basic_ostream<_CharT, _Traits>::operator<<(unsigned short __n)
419 #ifndef _LIBCPP_NO_EXCEPTIONS
420     try
421     {
422 #endif // _LIBCPP_NO_EXCEPTIONS
423         sentry __s(*this);
424         if (__s)
425         {
426             typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
427             const _Fp& __f = use_facet<_Fp>(this->getloc());
428             if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
429                 this->setstate(ios_base::badbit | ios_base::failbit);
430         }
431 #ifndef _LIBCPP_NO_EXCEPTIONS
432     }
433     catch (...)
434     {
435         this->__set_badbit_and_consider_rethrow();
436     }
437 #endif // _LIBCPP_NO_EXCEPTIONS
438     return *this;
441 template <class _CharT, class _Traits>
442 basic_ostream<_CharT, _Traits>&
443 basic_ostream<_CharT, _Traits>::operator<<(int __n)
445 #ifndef _LIBCPP_NO_EXCEPTIONS
446     try
447     {
448 #endif // _LIBCPP_NO_EXCEPTIONS
449         sentry __s(*this);
450         if (__s)
451         {
452             ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
453             typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
454             const _Fp& __f = use_facet<_Fp>(this->getloc());
455             if (__f.put(*this, *this, this->fill(),
456                         __flags == ios_base::oct || __flags == ios_base::hex ?
457                         static_cast<long>(static_cast<unsigned int>(__n))  :
458                         static_cast<long>(__n)).failed())
459                 this->setstate(ios_base::badbit | ios_base::failbit);
460         }
461 #ifndef _LIBCPP_NO_EXCEPTIONS
462     }
463     catch (...)
464     {
465         this->__set_badbit_and_consider_rethrow();
466     }
467 #endif // _LIBCPP_NO_EXCEPTIONS
468     return *this;
471 template <class _CharT, class _Traits>
472 basic_ostream<_CharT, _Traits>&
473 basic_ostream<_CharT, _Traits>::operator<<(unsigned int __n)
475 #ifndef _LIBCPP_NO_EXCEPTIONS
476     try
477     {
478 #endif // _LIBCPP_NO_EXCEPTIONS
479         sentry __s(*this);
480         if (__s)
481         {
482             typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
483             const _Fp& __f = use_facet<_Fp>(this->getloc());
484             if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
485                 this->setstate(ios_base::badbit | ios_base::failbit);
486         }
487 #ifndef _LIBCPP_NO_EXCEPTIONS
488     }
489     catch (...)
490     {
491         this->__set_badbit_and_consider_rethrow();
492     }
493 #endif // _LIBCPP_NO_EXCEPTIONS
494     return *this;
497 template <class _CharT, class _Traits>
498 basic_ostream<_CharT, _Traits>&
499 basic_ostream<_CharT, _Traits>::operator<<(long __n)
501 #ifndef _LIBCPP_NO_EXCEPTIONS
502     try
503     {
504 #endif // _LIBCPP_NO_EXCEPTIONS
505         sentry __s(*this);
506         if (__s)
507         {
508             typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
509             const _Fp& __f = use_facet<_Fp>(this->getloc());
510             if (__f.put(*this, *this, this->fill(), __n).failed())
511                 this->setstate(ios_base::badbit | ios_base::failbit);
512         }
513 #ifndef _LIBCPP_NO_EXCEPTIONS
514     }
515     catch (...)
516     {
517         this->__set_badbit_and_consider_rethrow();
518     }
519 #endif // _LIBCPP_NO_EXCEPTIONS
520     return *this;
523 template <class _CharT, class _Traits>
524 basic_ostream<_CharT, _Traits>&
525 basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n)
527 #ifndef _LIBCPP_NO_EXCEPTIONS
528     try
529     {
530 #endif // _LIBCPP_NO_EXCEPTIONS
531         sentry __s(*this);
532         if (__s)
533         {
534             typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
535             const _Fp& __f = use_facet<_Fp>(this->getloc());
536             if (__f.put(*this, *this, this->fill(), __n).failed())
537                 this->setstate(ios_base::badbit | ios_base::failbit);
538         }
539 #ifndef _LIBCPP_NO_EXCEPTIONS
540     }
541     catch (...)
542     {
543         this->__set_badbit_and_consider_rethrow();
544     }
545 #endif // _LIBCPP_NO_EXCEPTIONS
546     return *this;
549 template <class _CharT, class _Traits>
550 basic_ostream<_CharT, _Traits>&
551 basic_ostream<_CharT, _Traits>::operator<<(long long __n)
553 #ifndef _LIBCPP_NO_EXCEPTIONS
554     try
555     {
556 #endif // _LIBCPP_NO_EXCEPTIONS
557         sentry __s(*this);
558         if (__s)
559         {
560             typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
561             const _Fp& __f = use_facet<_Fp>(this->getloc());
562             if (__f.put(*this, *this, this->fill(), __n).failed())
563                 this->setstate(ios_base::badbit | ios_base::failbit);
564         }
565 #ifndef _LIBCPP_NO_EXCEPTIONS
566     }
567     catch (...)
568     {
569         this->__set_badbit_and_consider_rethrow();
570     }
571 #endif // _LIBCPP_NO_EXCEPTIONS
572     return *this;
575 template <class _CharT, class _Traits>
576 basic_ostream<_CharT, _Traits>&
577 basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n)
579 #ifndef _LIBCPP_NO_EXCEPTIONS
580     try
581     {
582 #endif // _LIBCPP_NO_EXCEPTIONS
583         sentry __s(*this);
584         if (__s)
585         {
586             typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
587             const _Fp& __f = use_facet<_Fp>(this->getloc());
588             if (__f.put(*this, *this, this->fill(), __n).failed())
589                 this->setstate(ios_base::badbit | ios_base::failbit);
590         }
591 #ifndef _LIBCPP_NO_EXCEPTIONS
592     }
593     catch (...)
594     {
595         this->__set_badbit_and_consider_rethrow();
596     }
597 #endif // _LIBCPP_NO_EXCEPTIONS
598     return *this;
601 template <class _CharT, class _Traits>
602 basic_ostream<_CharT, _Traits>&
603 basic_ostream<_CharT, _Traits>::operator<<(float __n)
605 #ifndef _LIBCPP_NO_EXCEPTIONS
606     try
607     {
608 #endif // _LIBCPP_NO_EXCEPTIONS
609         sentry __s(*this);
610         if (__s)
611         {
612             typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
613             const _Fp& __f = use_facet<_Fp>(this->getloc());
614             if (__f.put(*this, *this, this->fill(), static_cast<double>(__n)).failed())
615                 this->setstate(ios_base::badbit | ios_base::failbit);
616         }
617 #ifndef _LIBCPP_NO_EXCEPTIONS
618     }
619     catch (...)
620     {
621         this->__set_badbit_and_consider_rethrow();
622     }
623 #endif // _LIBCPP_NO_EXCEPTIONS
624     return *this;
627 template <class _CharT, class _Traits>
628 basic_ostream<_CharT, _Traits>&
629 basic_ostream<_CharT, _Traits>::operator<<(double __n)
631 #ifndef _LIBCPP_NO_EXCEPTIONS
632     try
633     {
634 #endif // _LIBCPP_NO_EXCEPTIONS
635         sentry __s(*this);
636         if (__s)
637         {
638             typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
639             const _Fp& __f = use_facet<_Fp>(this->getloc());
640             if (__f.put(*this, *this, this->fill(), __n).failed())
641                 this->setstate(ios_base::badbit | ios_base::failbit);
642         }
643 #ifndef _LIBCPP_NO_EXCEPTIONS
644     }
645     catch (...)
646     {
647         this->__set_badbit_and_consider_rethrow();
648     }
649 #endif // _LIBCPP_NO_EXCEPTIONS
650     return *this;
653 template <class _CharT, class _Traits>
654 basic_ostream<_CharT, _Traits>&
655 basic_ostream<_CharT, _Traits>::operator<<(long double __n)
657 #ifndef _LIBCPP_NO_EXCEPTIONS
658     try
659     {
660 #endif // _LIBCPP_NO_EXCEPTIONS
661         sentry __s(*this);
662         if (__s)
663         {
664             typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
665             const _Fp& __f = use_facet<_Fp>(this->getloc());
666             if (__f.put(*this, *this, this->fill(), __n).failed())
667                 this->setstate(ios_base::badbit | ios_base::failbit);
668         }
669 #ifndef _LIBCPP_NO_EXCEPTIONS
670     }
671     catch (...)
672     {
673         this->__set_badbit_and_consider_rethrow();
674     }
675 #endif // _LIBCPP_NO_EXCEPTIONS
676     return *this;
679 template <class _CharT, class _Traits>
680 basic_ostream<_CharT, _Traits>&
681 basic_ostream<_CharT, _Traits>::operator<<(const void* __n)
683 #ifndef _LIBCPP_NO_EXCEPTIONS
684     try
685     {
686 #endif // _LIBCPP_NO_EXCEPTIONS
687         sentry __s(*this);
688         if (__s)
689         {
690             typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
691             const _Fp& __f = use_facet<_Fp>(this->getloc());
692             if (__f.put(*this, *this, this->fill(), __n).failed())
693                 this->setstate(ios_base::badbit | ios_base::failbit);
694         }
695 #ifndef _LIBCPP_NO_EXCEPTIONS
696     }
697     catch (...)
698     {
699         this->__set_badbit_and_consider_rethrow();
700     }
701 #endif // _LIBCPP_NO_EXCEPTIONS
702     return *this;
705 template<class _CharT, class _Traits>
706 basic_ostream<_CharT, _Traits>&
707 __put_character_sequence(basic_ostream<_CharT, _Traits>& __os,
708                           const _CharT* __str, size_t __len)
710 #ifndef _LIBCPP_NO_EXCEPTIONS
711     try
712     {
713 #endif // _LIBCPP_NO_EXCEPTIONS
714         typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
715         if (__s)
716         {
717             typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
718             if (__pad_and_output(_Ip(__os),
719                                  __str,
720                                  (__os.flags() & ios_base::adjustfield) == ios_base::left ?
721                                      __str + __len :
722                                      __str,
723                                  __str + __len,
724                                  __os,
725                                  __os.fill()).failed())
726                 __os.setstate(ios_base::badbit | ios_base::failbit);
727         }
728 #ifndef _LIBCPP_NO_EXCEPTIONS
729     }
730     catch (...)
731     {
732         __os.__set_badbit_and_consider_rethrow();
733     }
734 #endif // _LIBCPP_NO_EXCEPTIONS
735     return __os;
739 template<class _CharT, class _Traits>
740 basic_ostream<_CharT, _Traits>&
741 operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c)
743     return _VSTD::__put_character_sequence(__os, &__c, 1);
746 template<class _CharT, class _Traits>
747 basic_ostream<_CharT, _Traits>&
748 operator<<(basic_ostream<_CharT, _Traits>& __os, char __cn)
750 #ifndef _LIBCPP_NO_EXCEPTIONS
751     try
752     {
753 #endif // _LIBCPP_NO_EXCEPTIONS
754         typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
755         if (__s)
756         {
757             _CharT __c = __os.widen(__cn);
758             typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
759             if (__pad_and_output(_Ip(__os),
760                                  &__c,
761                                  (__os.flags() & ios_base::adjustfield) == ios_base::left ?
762                                      &__c + 1 :
763                                      &__c,
764                                  &__c + 1,
765                                  __os,
766                                  __os.fill()).failed())
767                 __os.setstate(ios_base::badbit | ios_base::failbit);
768         }
769 #ifndef _LIBCPP_NO_EXCEPTIONS
770     }
771     catch (...)
772     {
773         __os.__set_badbit_and_consider_rethrow();
774     }
775 #endif // _LIBCPP_NO_EXCEPTIONS
776     return __os;
779 template<class _Traits>
780 basic_ostream<char, _Traits>&
781 operator<<(basic_ostream<char, _Traits>& __os, char __c)
783     return _VSTD::__put_character_sequence(__os, &__c, 1);
786 template<class _Traits>
787 basic_ostream<char, _Traits>&
788 operator<<(basic_ostream<char, _Traits>& __os, signed char __c)
790     return _VSTD::__put_character_sequence(__os, (char *) &__c, 1);
793 template<class _Traits>
794 basic_ostream<char, _Traits>&
795 operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c)
797     return _VSTD::__put_character_sequence(__os, (char *) &__c, 1);
800 template<class _CharT, class _Traits>
801 basic_ostream<_CharT, _Traits>&
802 operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str)
804     return _VSTD::__put_character_sequence(__os, __str, _Traits::length(__str));
807 template<class _CharT, class _Traits>
808 basic_ostream<_CharT, _Traits>&
809 operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __strn)
811 #ifndef _LIBCPP_NO_EXCEPTIONS
812     try
813     {
814 #endif // _LIBCPP_NO_EXCEPTIONS
815         typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
816         if (__s)
817         {
818             typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
819             size_t __len = char_traits<char>::length(__strn);
820             const int __bs = 100;
821             _CharT __wbb[__bs];
822             _CharT* __wb = __wbb;
823             unique_ptr<_CharT, void(*)(void*)> __h(0, free);
824             if (__len > __bs)
825             {
826                 __wb = (_CharT*)malloc(__len*sizeof(_CharT));
827                 if (__wb == 0)
828                     __throw_bad_alloc();
829                 __h.reset(__wb);
830             }
831             for (_CharT* __p = __wb; *__strn != '\0'; ++__strn, ++__p)
832                 *__p = __os.widen(*__strn);
833             if (__pad_and_output(_Ip(__os),
834                                  __wb,
835                                  (__os.flags() & ios_base::adjustfield) == ios_base::left ?
836                                      __wb + __len :
837                                      __wb,
838                                  __wb + __len,
839                                  __os,
840                                  __os.fill()).failed())
841                 __os.setstate(ios_base::badbit | ios_base::failbit);
842         }
843 #ifndef _LIBCPP_NO_EXCEPTIONS
844     }
845     catch (...)
846     {
847         __os.__set_badbit_and_consider_rethrow();
848     }
849 #endif // _LIBCPP_NO_EXCEPTIONS
850     return __os;
853 template<class _Traits>
854 basic_ostream<char, _Traits>&
855 operator<<(basic_ostream<char, _Traits>& __os, const char* __str)
857     return _VSTD::__put_character_sequence(__os, __str, _Traits::length(__str));
860 template<class _Traits>
861 basic_ostream<char, _Traits>&
862 operator<<(basic_ostream<char, _Traits>& __os, const signed char* __str)
864     const char *__s = (const char *) __str;
865     return _VSTD::__put_character_sequence(__os, __s, _Traits::length(__s));
868 template<class _Traits>
869 basic_ostream<char, _Traits>&
870 operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __str)
872     const char *__s = (const char *) __str;
873     return _VSTD::__put_character_sequence(__os, __s, _Traits::length(__s));
876 template <class _CharT, class _Traits>
877 basic_ostream<_CharT, _Traits>&
878 basic_ostream<_CharT, _Traits>::put(char_type __c)
880 #ifndef _LIBCPP_NO_EXCEPTIONS
881     try
882     {
883 #endif // _LIBCPP_NO_EXCEPTIONS
884         sentry __s(*this);
885         if (__s)
886         {
887             typedef ostreambuf_iterator<_CharT, _Traits> _Op;
888             _Op __o(*this);
889             *__o = __c;
890             if (__o.failed())
891                 this->setstate(ios_base::badbit);
892         }
893 #ifndef _LIBCPP_NO_EXCEPTIONS
894     }
895     catch (...)
896     {
897         this->__set_badbit_and_consider_rethrow();
898     }
899 #endif // _LIBCPP_NO_EXCEPTIONS
900     return *this;
903 template <class _CharT, class _Traits>
904 basic_ostream<_CharT, _Traits>&
905 basic_ostream<_CharT, _Traits>::write(const char_type* __s, streamsize __n)
907 #ifndef _LIBCPP_NO_EXCEPTIONS
908     try
909     {
910 #endif // _LIBCPP_NO_EXCEPTIONS
911         sentry __sen(*this);
912         if (__sen && __n)
913         {
914             if (this->rdbuf()->sputn(__s, __n) != __n)
915                 this->setstate(ios_base::badbit);
916         }
917 #ifndef _LIBCPP_NO_EXCEPTIONS
918     }
919     catch (...)
920     {
921         this->__set_badbit_and_consider_rethrow();
922     }
923 #endif // _LIBCPP_NO_EXCEPTIONS
924     return *this;
927 template <class _CharT, class _Traits>
928 basic_ostream<_CharT, _Traits>&
929 basic_ostream<_CharT, _Traits>::flush()
931 #ifndef _LIBCPP_NO_EXCEPTIONS
932     try
933     {
934 #endif // _LIBCPP_NO_EXCEPTIONS
935         if (this->rdbuf())
936         {
937             sentry __s(*this);
938             if (__s)
939             {
940                 if (this->rdbuf()->pubsync() == -1)
941                     this->setstate(ios_base::badbit);
942             }
943         }
944 #ifndef _LIBCPP_NO_EXCEPTIONS
945     }
946     catch (...)
947     {
948         this->__set_badbit_and_consider_rethrow();
949     }
950 #endif // _LIBCPP_NO_EXCEPTIONS
951     return *this;
954 template <class _CharT, class _Traits>
955 typename basic_ostream<_CharT, _Traits>::pos_type
956 basic_ostream<_CharT, _Traits>::tellp()
958     if (this->fail())
959         return pos_type(-1);
960     return this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
963 template <class _CharT, class _Traits>
964 basic_ostream<_CharT, _Traits>&
965 basic_ostream<_CharT, _Traits>::seekp(pos_type __pos)
967     sentry __s(*this);
968     if (!this->fail())
969     {
970         if (this->rdbuf()->pubseekpos(__pos, ios_base::out) == pos_type(-1))
971             this->setstate(ios_base::failbit);
972     }
973     return *this;
976 template <class _CharT, class _Traits>
977 basic_ostream<_CharT, _Traits>&
978 basic_ostream<_CharT, _Traits>::seekp(off_type __off, ios_base::seekdir __dir)
980     sentry __s(*this);
981     if (!this->fail())
982     {
983         if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::out) == pos_type(-1))
984             this->setstate(ios_base::failbit);
985     }
986     return *this;
989 template <class _CharT, class _Traits>
990 inline
991 basic_ostream<_CharT, _Traits>&
992 endl(basic_ostream<_CharT, _Traits>& __os)
994     __os.put(__os.widen('\n'));
995     __os.flush();
996     return __os;
999 template <class _CharT, class _Traits>
1000 inline
1001 basic_ostream<_CharT, _Traits>&
1002 ends(basic_ostream<_CharT, _Traits>& __os)
1004     __os.put(_CharT());
1005     return __os;
1008 template <class _CharT, class _Traits>
1009 inline
1010 basic_ostream<_CharT, _Traits>&
1011 flush(basic_ostream<_CharT, _Traits>& __os)
1013     __os.flush();
1014     return __os;
1017 template <class _Stream, class _Tp, class = void>
1018 struct __is_ostreamable : false_type { };
1020 template <class _Stream, class _Tp>
1021 struct __is_ostreamable<_Stream, _Tp, decltype(
1022     declval<_Stream>() << declval<_Tp>(), void()
1023 )> : true_type { };
1025 template <class _Stream, class _Tp, class = typename enable_if<
1026     _And<is_base_of<ios_base, _Stream>,
1027          __is_ostreamable<_Stream&, const _Tp&> >::value
1028 >::type>
1029 _LIBCPP_INLINE_VISIBILITY
1030 _Stream&& operator<<(_Stream&& __os, const _Tp& __x)
1032     __os << __x;
1033     return _VSTD::move(__os);
1036 template<class _CharT, class _Traits, class _Allocator>
1037 basic_ostream<_CharT, _Traits>&
1038 operator<<(basic_ostream<_CharT, _Traits>& __os,
1039            const basic_string<_CharT, _Traits, _Allocator>& __str)
1041     return _VSTD::__put_character_sequence(__os, __str.data(), __str.size());
1044 template<class _CharT, class _Traits>
1045 basic_ostream<_CharT, _Traits>&
1046 operator<<(basic_ostream<_CharT, _Traits>& __os,
1047            basic_string_view<_CharT, _Traits> __sv)
1049     return _VSTD::__put_character_sequence(__os, __sv.data(), __sv.size());
1052 template <class _CharT, class _Traits>
1053 inline _LIBCPP_INLINE_VISIBILITY
1054 basic_ostream<_CharT, _Traits>&
1055 operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __ec)
1057     return __os << __ec.category().name() << ':' << __ec.value();
1060 template<class _CharT, class _Traits, class _Yp>
1061 inline _LIBCPP_INLINE_VISIBILITY
1062 basic_ostream<_CharT, _Traits>&
1063 operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p)
1065     return __os << __p.get();
1068 template<class _CharT, class _Traits, class _Yp, class _Dp>
1069 inline _LIBCPP_INLINE_VISIBILITY
1070 typename enable_if
1072     is_same<void, typename __void_t<decltype((declval<basic_ostream<_CharT, _Traits>&>() << declval<typename unique_ptr<_Yp, _Dp>::pointer>()))>::type>::value,
1073     basic_ostream<_CharT, _Traits>&
1074 >::type
1075 operator<<(basic_ostream<_CharT, _Traits>& __os, unique_ptr<_Yp, _Dp> const& __p)
1077     return __os << __p.get();
1080 template <class _CharT, class _Traits, size_t _Size>
1081 basic_ostream<_CharT, _Traits>&
1082 operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x)
1084     return __os << __x.template to_string<_CharT, _Traits>
1085                         (use_facet<ctype<_CharT> >(__os.getloc()).widen('0'),
1086                          use_facet<ctype<_CharT> >(__os.getloc()).widen('1'));
1089 _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<char>)
1090 _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<wchar_t>)
1092 _LIBCPP_END_NAMESPACE_STD
1094 #endif // _LIBCPP_OSTREAM