1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
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//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_OSTREAM
11#define _LIBCPP_OSTREAM
12
13/*
14    ostream synopsis
15
16template <class charT, class traits = char_traits<charT> >
17class basic_ostream
18    : virtual public basic_ios<charT,traits>
19{
20public:
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;
27
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();
32
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);
37
38    // 27.7.2.4 Prefix/suffix:
39    class sentry;
40
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<<(const volatile void* val); // C++23
59    basic_ostream& operator<<(basic_streambuf<char_type,traits>* sb);
60    basic_ostream& operator<<(nullptr_t);
61
62    // 27.7.2.7 Unformatted output:
63    basic_ostream& put(char_type c);
64    basic_ostream& write(const char_type* s, streamsize n);
65    basic_ostream& flush();
66
67    // 27.7.2.5 seeks:
68    pos_type tellp();
69    basic_ostream& seekp(pos_type);
70    basic_ostream& seekp(off_type, ios_base::seekdir);
71protected:
72    basic_ostream(const basic_ostream& rhs) = delete;
73    basic_ostream(basic_ostream&& rhs);
74    // 27.7.3.3 Assign/swap
75    basic_ostream& operator=(basic_ostream& rhs) = delete;
76    basic_ostream& operator=(const basic_ostream&& rhs);
77    void swap(basic_ostream& rhs);
78};
79
80// 27.7.2.6.4 character inserters
81
82template<class charT, class traits>
83  basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, charT);
84
85template<class charT, class traits>
86  basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, char);
87
88template<class traits>
89  basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, char);
90
91// signed and unsigned
92
93template<class traits>
94  basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, signed char);
95
96template<class traits>
97  basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, unsigned char);
98
99// NTBS
100template<class charT, class traits>
101  basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, const charT*);
102
103template<class charT, class traits>
104  basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, const char*);
105
106template<class traits>
107  basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, const char*);
108
109// signed and unsigned
110template<class traits>
111basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, const signed char*);
112
113template<class traits>
114  basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, const unsigned char*);
115
116// swap:
117template <class charT, class traits>
118  void swap(basic_ostream<charT, traits>& x, basic_ostream<charT, traits>& y);
119
120template <class charT, class traits>
121  basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);
122
123template <class charT, class traits>
124  basic_ostream<charT,traits>& ends(basic_ostream<charT,traits>& os);
125
126template <class charT, class traits>
127  basic_ostream<charT,traits>& flush(basic_ostream<charT,traits>& os);
128
129// rvalue stream insertion
130template <class Stream, class T>
131  Stream&& operator<<(Stream&& os, const T& x);
132
133}  // std
134
135*/
136
137#include <__assert> // all public C++ headers provide the assertion handler
138#include <__config>
139#include <bitset>
140#include <ios>
141#include <locale>
142#include <streambuf>
143#include <version>
144
145#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
146#  include <iterator>
147#endif
148
149#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
150#  pragma GCC system_header
151#endif
152
153_LIBCPP_BEGIN_NAMESPACE_STD
154
155template <class _CharT, class _Traits>
156class _LIBCPP_TEMPLATE_VIS basic_ostream
157    : virtual public basic_ios<_CharT, _Traits>
158{
159public:
160    // types (inherited from basic_ios (27.5.4)):
161    typedef _CharT                         char_type;
162    typedef _Traits                        traits_type;
163    typedef typename traits_type::int_type int_type;
164    typedef typename traits_type::pos_type pos_type;
165    typedef typename traits_type::off_type off_type;
166
167    // 27.7.2.2 Constructor/destructor:
168    inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
169    explicit basic_ostream(basic_streambuf<char_type, traits_type>* __sb)
170    { this->init(__sb); }
171    virtual ~basic_ostream();
172protected:
173    inline _LIBCPP_INLINE_VISIBILITY
174    basic_ostream(basic_ostream&& __rhs);
175
176    // 27.7.2.3 Assign/swap
177    inline _LIBCPP_INLINE_VISIBILITY
178    basic_ostream& operator=(basic_ostream&& __rhs);
179
180    inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
181    void swap(basic_ostream& __rhs)
182    { basic_ios<char_type, traits_type>::swap(__rhs); }
183
184    basic_ostream           (const basic_ostream& __rhs) = delete;
185    basic_ostream& operator=(const basic_ostream& __rhs) = delete;
186
187public:
188    // 27.7.2.4 Prefix/suffix:
189    class _LIBCPP_TEMPLATE_VIS sentry;
190
191    // 27.7.2.6 Formatted output:
192    inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
193    basic_ostream& operator<<(basic_ostream& (*__pf)(basic_ostream&))
194    { return __pf(*this); }
195
196    inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
197    basic_ostream& operator<<(basic_ios<char_type, traits_type>&
198                              (*__pf)(basic_ios<char_type,traits_type>&))
199    { __pf(*this); return *this; }
200
201    inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
202    basic_ostream& operator<<(ios_base& (*__pf)(ios_base&))
203    { __pf(*this); return *this; }
204
205    basic_ostream& operator<<(bool __n);
206    basic_ostream& operator<<(short __n);
207    basic_ostream& operator<<(unsigned short __n);
208    basic_ostream& operator<<(int __n);
209    basic_ostream& operator<<(unsigned int __n);
210    basic_ostream& operator<<(long __n);
211    basic_ostream& operator<<(unsigned long __n);
212    basic_ostream& operator<<(long long __n);
213    basic_ostream& operator<<(unsigned long long __n);
214    basic_ostream& operator<<(float __f);
215    basic_ostream& operator<<(double __f);
216    basic_ostream& operator<<(long double __f);
217    basic_ostream& operator<<(const void* __p);
218
219#if _LIBCPP_STD_VER > 20
220    _LIBCPP_HIDE_FROM_ABI
221    basic_ostream& operator<<(const volatile void* __p) {
222        return operator<<(const_cast<const void*>(__p));
223    }
224#endif
225
226    basic_ostream& operator<<(basic_streambuf<char_type, traits_type>* __sb);
227
228#if _LIBCPP_STD_VER > 14
229// LWG 2221 - nullptr. This is not backported to older standards modes.
230// See https://reviews.llvm.org/D127033 for more info on the rationale.
231    _LIBCPP_INLINE_VISIBILITY
232    basic_ostream& operator<<(nullptr_t)
233    { return *this << "nullptr"; }
234#endif
235
236    // 27.7.2.7 Unformatted output:
237    basic_ostream& put(char_type __c);
238    basic_ostream& write(const char_type* __s, streamsize __n);
239    basic_ostream& flush();
240
241    // 27.7.2.5 seeks:
242    inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
243    pos_type tellp();
244    inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
245    basic_ostream& seekp(pos_type __pos);
246    inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
247    basic_ostream& seekp(off_type __off, ios_base::seekdir __dir);
248
249protected:
250    _LIBCPP_INLINE_VISIBILITY
251    basic_ostream() {}  // extension, intentially does not initialize
252};
253
254template <class _CharT, class _Traits>
255class _LIBCPP_TEMPLATE_VIS basic_ostream<_CharT, _Traits>::sentry
256{
257    bool __ok_;
258    basic_ostream<_CharT, _Traits>& __os_;
259
260public:
261    explicit sentry(basic_ostream<_CharT, _Traits>& __os);
262    ~sentry();
263    sentry(const sentry&) = delete;
264    sentry& operator=(const sentry&) = delete;
265
266    _LIBCPP_INLINE_VISIBILITY
267    explicit operator bool() const {return __ok_;}
268};
269
270template <class _CharT, class _Traits>
271basic_ostream<_CharT, _Traits>::sentry::sentry(basic_ostream<_CharT, _Traits>& __os)
272    : __ok_(false),
273      __os_(__os)
274{
275    if (__os.good())
276    {
277        if (__os.tie())
278            __os.tie()->flush();
279        __ok_ = true;
280    }
281}
282
283template <class _CharT, class _Traits>
284basic_ostream<_CharT, _Traits>::sentry::~sentry()
285{
286    if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf)
287                      && !uncaught_exception())
288    {
289#ifndef _LIBCPP_NO_EXCEPTIONS
290        try
291        {
292#endif // _LIBCPP_NO_EXCEPTIONS
293            if (__os_.rdbuf()->pubsync() == -1)
294                __os_.setstate(ios_base::badbit);
295#ifndef _LIBCPP_NO_EXCEPTIONS
296        }
297        catch (...)
298        {
299        }
300#endif // _LIBCPP_NO_EXCEPTIONS
301    }
302}
303
304template <class _CharT, class _Traits>
305basic_ostream<_CharT, _Traits>::basic_ostream(basic_ostream&& __rhs)
306{
307    this->move(__rhs);
308}
309
310template <class _CharT, class _Traits>
311basic_ostream<_CharT, _Traits>&
312basic_ostream<_CharT, _Traits>::operator=(basic_ostream&& __rhs)
313{
314    swap(__rhs);
315    return *this;
316}
317
318template <class _CharT, class _Traits>
319basic_ostream<_CharT, _Traits>::~basic_ostream()
320{
321}
322
323template <class _CharT, class _Traits>
324basic_ostream<_CharT, _Traits>&
325basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<char_type, traits_type>* __sb)
326{
327#ifndef _LIBCPP_NO_EXCEPTIONS
328    try
329    {
330#endif // _LIBCPP_NO_EXCEPTIONS
331        sentry __s(*this);
332        if (__s)
333        {
334            if (__sb)
335            {
336#ifndef _LIBCPP_NO_EXCEPTIONS
337                try
338                {
339#endif // _LIBCPP_NO_EXCEPTIONS
340                    typedef istreambuf_iterator<_CharT, _Traits> _Ip;
341                    typedef ostreambuf_iterator<_CharT, _Traits> _Op;
342                    _Ip __i(__sb);
343                    _Ip __eof;
344                    _Op __o(*this);
345                    size_t __c = 0;
346                    for (; __i != __eof; ++__i, ++__o, ++__c)
347                    {
348                        *__o = *__i;
349                        if (__o.failed())
350                            break;
351                    }
352                    if (__c == 0)
353                        this->setstate(ios_base::failbit);
354#ifndef _LIBCPP_NO_EXCEPTIONS
355                }
356                catch (...)
357                {
358                    this->__set_failbit_and_consider_rethrow();
359                }
360#endif // _LIBCPP_NO_EXCEPTIONS
361            }
362            else
363                this->setstate(ios_base::badbit);
364        }
365#ifndef _LIBCPP_NO_EXCEPTIONS
366    }
367    catch (...)
368    {
369        this->__set_badbit_and_consider_rethrow();
370    }
371#endif // _LIBCPP_NO_EXCEPTIONS
372    return *this;
373}
374
375template <class _CharT, class _Traits>
376basic_ostream<_CharT, _Traits>&
377basic_ostream<_CharT, _Traits>::operator<<(bool __n)
378{
379#ifndef _LIBCPP_NO_EXCEPTIONS
380    try
381    {
382#endif // _LIBCPP_NO_EXCEPTIONS
383        sentry __s(*this);
384        if (__s)
385        {
386            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
387            const _Fp& __f = use_facet<_Fp>(this->getloc());
388            if (__f.put(*this, *this, this->fill(), __n).failed())
389                this->setstate(ios_base::badbit | ios_base::failbit);
390        }
391#ifndef _LIBCPP_NO_EXCEPTIONS
392    }
393    catch (...)
394    {
395        this->__set_badbit_and_consider_rethrow();
396    }
397#endif // _LIBCPP_NO_EXCEPTIONS
398    return *this;
399}
400
401template <class _CharT, class _Traits>
402basic_ostream<_CharT, _Traits>&
403basic_ostream<_CharT, _Traits>::operator<<(short __n)
404{
405#ifndef _LIBCPP_NO_EXCEPTIONS
406    try
407    {
408#endif // _LIBCPP_NO_EXCEPTIONS
409        sentry __s(*this);
410        if (__s)
411        {
412            ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
413            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
414            const _Fp& __f = use_facet<_Fp>(this->getloc());
415            if (__f.put(*this, *this, this->fill(),
416                        __flags == ios_base::oct || __flags == ios_base::hex ?
417                        static_cast<long>(static_cast<unsigned short>(__n))  :
418                        static_cast<long>(__n)).failed())
419                this->setstate(ios_base::badbit | ios_base::failbit);
420        }
421#ifndef _LIBCPP_NO_EXCEPTIONS
422    }
423    catch (...)
424    {
425        this->__set_badbit_and_consider_rethrow();
426    }
427#endif // _LIBCPP_NO_EXCEPTIONS
428    return *this;
429}
430
431template <class _CharT, class _Traits>
432basic_ostream<_CharT, _Traits>&
433basic_ostream<_CharT, _Traits>::operator<<(unsigned short __n)
434{
435#ifndef _LIBCPP_NO_EXCEPTIONS
436    try
437    {
438#endif // _LIBCPP_NO_EXCEPTIONS
439        sentry __s(*this);
440        if (__s)
441        {
442            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
443            const _Fp& __f = use_facet<_Fp>(this->getloc());
444            if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
445                this->setstate(ios_base::badbit | ios_base::failbit);
446        }
447#ifndef _LIBCPP_NO_EXCEPTIONS
448    }
449    catch (...)
450    {
451        this->__set_badbit_and_consider_rethrow();
452    }
453#endif // _LIBCPP_NO_EXCEPTIONS
454    return *this;
455}
456
457template <class _CharT, class _Traits>
458basic_ostream<_CharT, _Traits>&
459basic_ostream<_CharT, _Traits>::operator<<(int __n)
460{
461#ifndef _LIBCPP_NO_EXCEPTIONS
462    try
463    {
464#endif // _LIBCPP_NO_EXCEPTIONS
465        sentry __s(*this);
466        if (__s)
467        {
468            ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
469            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
470            const _Fp& __f = use_facet<_Fp>(this->getloc());
471            if (__f.put(*this, *this, this->fill(),
472                        __flags == ios_base::oct || __flags == ios_base::hex ?
473                        static_cast<long>(static_cast<unsigned int>(__n))  :
474                        static_cast<long>(__n)).failed())
475                this->setstate(ios_base::badbit | ios_base::failbit);
476        }
477#ifndef _LIBCPP_NO_EXCEPTIONS
478    }
479    catch (...)
480    {
481        this->__set_badbit_and_consider_rethrow();
482    }
483#endif // _LIBCPP_NO_EXCEPTIONS
484    return *this;
485}
486
487template <class _CharT, class _Traits>
488basic_ostream<_CharT, _Traits>&
489basic_ostream<_CharT, _Traits>::operator<<(unsigned int __n)
490{
491#ifndef _LIBCPP_NO_EXCEPTIONS
492    try
493    {
494#endif // _LIBCPP_NO_EXCEPTIONS
495        sentry __s(*this);
496        if (__s)
497        {
498            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
499            const _Fp& __f = use_facet<_Fp>(this->getloc());
500            if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
501                this->setstate(ios_base::badbit | ios_base::failbit);
502        }
503#ifndef _LIBCPP_NO_EXCEPTIONS
504    }
505    catch (...)
506    {
507        this->__set_badbit_and_consider_rethrow();
508    }
509#endif // _LIBCPP_NO_EXCEPTIONS
510    return *this;
511}
512
513template <class _CharT, class _Traits>
514basic_ostream<_CharT, _Traits>&
515basic_ostream<_CharT, _Traits>::operator<<(long __n)
516{
517#ifndef _LIBCPP_NO_EXCEPTIONS
518    try
519    {
520#endif // _LIBCPP_NO_EXCEPTIONS
521        sentry __s(*this);
522        if (__s)
523        {
524            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
525            const _Fp& __f = use_facet<_Fp>(this->getloc());
526            if (__f.put(*this, *this, this->fill(), __n).failed())
527                this->setstate(ios_base::badbit | ios_base::failbit);
528        }
529#ifndef _LIBCPP_NO_EXCEPTIONS
530    }
531    catch (...)
532    {
533        this->__set_badbit_and_consider_rethrow();
534    }
535#endif // _LIBCPP_NO_EXCEPTIONS
536    return *this;
537}
538
539template <class _CharT, class _Traits>
540basic_ostream<_CharT, _Traits>&
541basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n)
542{
543#ifndef _LIBCPP_NO_EXCEPTIONS
544    try
545    {
546#endif // _LIBCPP_NO_EXCEPTIONS
547        sentry __s(*this);
548        if (__s)
549        {
550            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
551            const _Fp& __f = use_facet<_Fp>(this->getloc());
552            if (__f.put(*this, *this, this->fill(), __n).failed())
553                this->setstate(ios_base::badbit | ios_base::failbit);
554        }
555#ifndef _LIBCPP_NO_EXCEPTIONS
556    }
557    catch (...)
558    {
559        this->__set_badbit_and_consider_rethrow();
560    }
561#endif // _LIBCPP_NO_EXCEPTIONS
562    return *this;
563}
564
565template <class _CharT, class _Traits>
566basic_ostream<_CharT, _Traits>&
567basic_ostream<_CharT, _Traits>::operator<<(long long __n)
568{
569#ifndef _LIBCPP_NO_EXCEPTIONS
570    try
571    {
572#endif // _LIBCPP_NO_EXCEPTIONS
573        sentry __s(*this);
574        if (__s)
575        {
576            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
577            const _Fp& __f = use_facet<_Fp>(this->getloc());
578            if (__f.put(*this, *this, this->fill(), __n).failed())
579                this->setstate(ios_base::badbit | ios_base::failbit);
580        }
581#ifndef _LIBCPP_NO_EXCEPTIONS
582    }
583    catch (...)
584    {
585        this->__set_badbit_and_consider_rethrow();
586    }
587#endif // _LIBCPP_NO_EXCEPTIONS
588    return *this;
589}
590
591template <class _CharT, class _Traits>
592basic_ostream<_CharT, _Traits>&
593basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n)
594{
595#ifndef _LIBCPP_NO_EXCEPTIONS
596    try
597    {
598#endif // _LIBCPP_NO_EXCEPTIONS
599        sentry __s(*this);
600        if (__s)
601        {
602            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
603            const _Fp& __f = use_facet<_Fp>(this->getloc());
604            if (__f.put(*this, *this, this->fill(), __n).failed())
605                this->setstate(ios_base::badbit | ios_base::failbit);
606        }
607#ifndef _LIBCPP_NO_EXCEPTIONS
608    }
609    catch (...)
610    {
611        this->__set_badbit_and_consider_rethrow();
612    }
613#endif // _LIBCPP_NO_EXCEPTIONS
614    return *this;
615}
616
617template <class _CharT, class _Traits>
618basic_ostream<_CharT, _Traits>&
619basic_ostream<_CharT, _Traits>::operator<<(float __n)
620{
621#ifndef _LIBCPP_NO_EXCEPTIONS
622    try
623    {
624#endif // _LIBCPP_NO_EXCEPTIONS
625        sentry __s(*this);
626        if (__s)
627        {
628            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
629            const _Fp& __f = use_facet<_Fp>(this->getloc());
630            if (__f.put(*this, *this, this->fill(), static_cast<double>(__n)).failed())
631                this->setstate(ios_base::badbit | ios_base::failbit);
632        }
633#ifndef _LIBCPP_NO_EXCEPTIONS
634    }
635    catch (...)
636    {
637        this->__set_badbit_and_consider_rethrow();
638    }
639#endif // _LIBCPP_NO_EXCEPTIONS
640    return *this;
641}
642
643template <class _CharT, class _Traits>
644basic_ostream<_CharT, _Traits>&
645basic_ostream<_CharT, _Traits>::operator<<(double __n)
646{
647#ifndef _LIBCPP_NO_EXCEPTIONS
648    try
649    {
650#endif // _LIBCPP_NO_EXCEPTIONS
651        sentry __s(*this);
652        if (__s)
653        {
654            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
655            const _Fp& __f = use_facet<_Fp>(this->getloc());
656            if (__f.put(*this, *this, this->fill(), __n).failed())
657                this->setstate(ios_base::badbit | ios_base::failbit);
658        }
659#ifndef _LIBCPP_NO_EXCEPTIONS
660    }
661    catch (...)
662    {
663        this->__set_badbit_and_consider_rethrow();
664    }
665#endif // _LIBCPP_NO_EXCEPTIONS
666    return *this;
667}
668
669template <class _CharT, class _Traits>
670basic_ostream<_CharT, _Traits>&
671basic_ostream<_CharT, _Traits>::operator<<(long double __n)
672{
673#ifndef _LIBCPP_NO_EXCEPTIONS
674    try
675    {
676#endif // _LIBCPP_NO_EXCEPTIONS
677        sentry __s(*this);
678        if (__s)
679        {
680            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
681            const _Fp& __f = use_facet<_Fp>(this->getloc());
682            if (__f.put(*this, *this, this->fill(), __n).failed())
683                this->setstate(ios_base::badbit | ios_base::failbit);
684        }
685#ifndef _LIBCPP_NO_EXCEPTIONS
686    }
687    catch (...)
688    {
689        this->__set_badbit_and_consider_rethrow();
690    }
691#endif // _LIBCPP_NO_EXCEPTIONS
692    return *this;
693}
694
695template <class _CharT, class _Traits>
696basic_ostream<_CharT, _Traits>&
697basic_ostream<_CharT, _Traits>::operator<<(const void* __n)
698{
699#ifndef _LIBCPP_NO_EXCEPTIONS
700    try
701    {
702#endif // _LIBCPP_NO_EXCEPTIONS
703        sentry __s(*this);
704        if (__s)
705        {
706            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
707            const _Fp& __f = use_facet<_Fp>(this->getloc());
708            if (__f.put(*this, *this, this->fill(), __n).failed())
709                this->setstate(ios_base::badbit | ios_base::failbit);
710        }
711#ifndef _LIBCPP_NO_EXCEPTIONS
712    }
713    catch (...)
714    {
715        this->__set_badbit_and_consider_rethrow();
716    }
717#endif // _LIBCPP_NO_EXCEPTIONS
718    return *this;
719}
720
721template<class _CharT, class _Traits>
722basic_ostream<_CharT, _Traits>&
723__put_character_sequence(basic_ostream<_CharT, _Traits>& __os,
724                          const _CharT* __str, size_t __len)
725{
726#ifndef _LIBCPP_NO_EXCEPTIONS
727    try
728    {
729#endif // _LIBCPP_NO_EXCEPTIONS
730        typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
731        if (__s)
732        {
733            typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
734            if (__pad_and_output(_Ip(__os),
735                                 __str,
736                                 (__os.flags() & ios_base::adjustfield) == ios_base::left ?
737                                     __str + __len :
738                                     __str,
739                                 __str + __len,
740                                 __os,
741                                 __os.fill()).failed())
742                __os.setstate(ios_base::badbit | ios_base::failbit);
743        }
744#ifndef _LIBCPP_NO_EXCEPTIONS
745    }
746    catch (...)
747    {
748        __os.__set_badbit_and_consider_rethrow();
749    }
750#endif // _LIBCPP_NO_EXCEPTIONS
751    return __os;
752}
753
754
755template<class _CharT, class _Traits>
756basic_ostream<_CharT, _Traits>&
757operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c)
758{
759    return _VSTD::__put_character_sequence(__os, &__c, 1);
760}
761
762template<class _CharT, class _Traits>
763basic_ostream<_CharT, _Traits>&
764operator<<(basic_ostream<_CharT, _Traits>& __os, char __cn)
765{
766#ifndef _LIBCPP_NO_EXCEPTIONS
767    try
768    {
769#endif // _LIBCPP_NO_EXCEPTIONS
770        typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
771        if (__s)
772        {
773            _CharT __c = __os.widen(__cn);
774            typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
775            if (__pad_and_output(_Ip(__os),
776                                 &__c,
777                                 (__os.flags() & ios_base::adjustfield) == ios_base::left ?
778                                     &__c + 1 :
779                                     &__c,
780                                 &__c + 1,
781                                 __os,
782                                 __os.fill()).failed())
783                __os.setstate(ios_base::badbit | ios_base::failbit);
784        }
785#ifndef _LIBCPP_NO_EXCEPTIONS
786    }
787    catch (...)
788    {
789        __os.__set_badbit_and_consider_rethrow();
790    }
791#endif // _LIBCPP_NO_EXCEPTIONS
792    return __os;
793}
794
795template<class _Traits>
796basic_ostream<char, _Traits>&
797operator<<(basic_ostream<char, _Traits>& __os, char __c)
798{
799    return _VSTD::__put_character_sequence(__os, &__c, 1);
800}
801
802template<class _Traits>
803basic_ostream<char, _Traits>&
804operator<<(basic_ostream<char, _Traits>& __os, signed char __c)
805{
806    return _VSTD::__put_character_sequence(__os, (char *) &__c, 1);
807}
808
809template<class _Traits>
810basic_ostream<char, _Traits>&
811operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c)
812{
813    return _VSTD::__put_character_sequence(__os, (char *) &__c, 1);
814}
815
816template<class _CharT, class _Traits>
817basic_ostream<_CharT, _Traits>&
818operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str)
819{
820    return _VSTD::__put_character_sequence(__os, __str, _Traits::length(__str));
821}
822
823template<class _CharT, class _Traits>
824basic_ostream<_CharT, _Traits>&
825operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __strn)
826{
827#ifndef _LIBCPP_NO_EXCEPTIONS
828    try
829    {
830#endif // _LIBCPP_NO_EXCEPTIONS
831        typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
832        if (__s)
833        {
834            typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
835            size_t __len = char_traits<char>::length(__strn);
836            const int __bs = 100;
837            _CharT __wbb[__bs];
838            _CharT* __wb = __wbb;
839            unique_ptr<_CharT, void(*)(void*)> __h(0, free);
840            if (__len > __bs)
841            {
842                __wb = (_CharT*)malloc(__len*sizeof(_CharT));
843                if (__wb == 0)
844                    __throw_bad_alloc();
845                __h.reset(__wb);
846            }
847            for (_CharT* __p = __wb; *__strn != '\0'; ++__strn, ++__p)
848                *__p = __os.widen(*__strn);
849            if (__pad_and_output(_Ip(__os),
850                                 __wb,
851                                 (__os.flags() & ios_base::adjustfield) == ios_base::left ?
852                                     __wb + __len :
853                                     __wb,
854                                 __wb + __len,
855                                 __os,
856                                 __os.fill()).failed())
857                __os.setstate(ios_base::badbit | ios_base::failbit);
858        }
859#ifndef _LIBCPP_NO_EXCEPTIONS
860    }
861    catch (...)
862    {
863        __os.__set_badbit_and_consider_rethrow();
864    }
865#endif // _LIBCPP_NO_EXCEPTIONS
866    return __os;
867}
868
869template<class _Traits>
870basic_ostream<char, _Traits>&
871operator<<(basic_ostream<char, _Traits>& __os, const char* __str)
872{
873    return _VSTD::__put_character_sequence(__os, __str, _Traits::length(__str));
874}
875
876template<class _Traits>
877basic_ostream<char, _Traits>&
878operator<<(basic_ostream<char, _Traits>& __os, const signed char* __str)
879{
880    const char *__s = (const char *) __str;
881    return _VSTD::__put_character_sequence(__os, __s, _Traits::length(__s));
882}
883
884template<class _Traits>
885basic_ostream<char, _Traits>&
886operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __str)
887{
888    const char *__s = (const char *) __str;
889    return _VSTD::__put_character_sequence(__os, __s, _Traits::length(__s));
890}
891
892template <class _CharT, class _Traits>
893basic_ostream<_CharT, _Traits>&
894basic_ostream<_CharT, _Traits>::put(char_type __c)
895{
896#ifndef _LIBCPP_NO_EXCEPTIONS
897    try
898    {
899#endif // _LIBCPP_NO_EXCEPTIONS
900        sentry __s(*this);
901        if (__s)
902        {
903            typedef ostreambuf_iterator<_CharT, _Traits> _Op;
904            _Op __o(*this);
905            *__o = __c;
906            if (__o.failed())
907                this->setstate(ios_base::badbit);
908        }
909#ifndef _LIBCPP_NO_EXCEPTIONS
910    }
911    catch (...)
912    {
913        this->__set_badbit_and_consider_rethrow();
914    }
915#endif // _LIBCPP_NO_EXCEPTIONS
916    return *this;
917}
918
919template <class _CharT, class _Traits>
920basic_ostream<_CharT, _Traits>&
921basic_ostream<_CharT, _Traits>::write(const char_type* __s, streamsize __n)
922{
923#ifndef _LIBCPP_NO_EXCEPTIONS
924    try
925    {
926#endif // _LIBCPP_NO_EXCEPTIONS
927        sentry __sen(*this);
928        if (__sen && __n)
929        {
930            if (this->rdbuf()->sputn(__s, __n) != __n)
931                this->setstate(ios_base::badbit);
932        }
933#ifndef _LIBCPP_NO_EXCEPTIONS
934    }
935    catch (...)
936    {
937        this->__set_badbit_and_consider_rethrow();
938    }
939#endif // _LIBCPP_NO_EXCEPTIONS
940    return *this;
941}
942
943template <class _CharT, class _Traits>
944basic_ostream<_CharT, _Traits>&
945basic_ostream<_CharT, _Traits>::flush()
946{
947#ifndef _LIBCPP_NO_EXCEPTIONS
948    try
949    {
950#endif // _LIBCPP_NO_EXCEPTIONS
951        if (this->rdbuf())
952        {
953            sentry __s(*this);
954            if (__s)
955            {
956                if (this->rdbuf()->pubsync() == -1)
957                    this->setstate(ios_base::badbit);
958            }
959        }
960#ifndef _LIBCPP_NO_EXCEPTIONS
961    }
962    catch (...)
963    {
964        this->__set_badbit_and_consider_rethrow();
965    }
966#endif // _LIBCPP_NO_EXCEPTIONS
967    return *this;
968}
969
970template <class _CharT, class _Traits>
971typename basic_ostream<_CharT, _Traits>::pos_type
972basic_ostream<_CharT, _Traits>::tellp()
973{
974    if (this->fail())
975        return pos_type(-1);
976    return this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
977}
978
979template <class _CharT, class _Traits>
980basic_ostream<_CharT, _Traits>&
981basic_ostream<_CharT, _Traits>::seekp(pos_type __pos)
982{
983    sentry __s(*this);
984    if (!this->fail())
985    {
986        if (this->rdbuf()->pubseekpos(__pos, ios_base::out) == pos_type(-1))
987            this->setstate(ios_base::failbit);
988    }
989    return *this;
990}
991
992template <class _CharT, class _Traits>
993basic_ostream<_CharT, _Traits>&
994basic_ostream<_CharT, _Traits>::seekp(off_type __off, ios_base::seekdir __dir)
995{
996    sentry __s(*this);
997    if (!this->fail())
998    {
999        if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::out) == pos_type(-1))
1000            this->setstate(ios_base::failbit);
1001    }
1002    return *this;
1003}
1004
1005template <class _CharT, class _Traits>
1006inline
1007basic_ostream<_CharT, _Traits>&
1008endl(basic_ostream<_CharT, _Traits>& __os)
1009{
1010    __os.put(__os.widen('\n'));
1011    __os.flush();
1012    return __os;
1013}
1014
1015template <class _CharT, class _Traits>
1016inline
1017basic_ostream<_CharT, _Traits>&
1018ends(basic_ostream<_CharT, _Traits>& __os)
1019{
1020    __os.put(_CharT());
1021    return __os;
1022}
1023
1024template <class _CharT, class _Traits>
1025inline
1026basic_ostream<_CharT, _Traits>&
1027flush(basic_ostream<_CharT, _Traits>& __os)
1028{
1029    __os.flush();
1030    return __os;
1031}
1032
1033template <class _Stream, class _Tp, class = void>
1034struct __is_ostreamable : false_type { };
1035
1036template <class _Stream, class _Tp>
1037struct __is_ostreamable<_Stream, _Tp, decltype(
1038    declval<_Stream>() << declval<_Tp>(), void()
1039)> : true_type { };
1040
1041template <class _Stream, class _Tp, class = typename enable_if<
1042    _And<is_base_of<ios_base, _Stream>,
1043         __is_ostreamable<_Stream&, const _Tp&> >::value
1044>::type>
1045_LIBCPP_INLINE_VISIBILITY
1046_Stream&& operator<<(_Stream&& __os, const _Tp& __x)
1047{
1048    __os << __x;
1049    return _VSTD::move(__os);
1050}
1051
1052template<class _CharT, class _Traits, class _Allocator>
1053basic_ostream<_CharT, _Traits>&
1054operator<<(basic_ostream<_CharT, _Traits>& __os,
1055           const basic_string<_CharT, _Traits, _Allocator>& __str)
1056{
1057    return _VSTD::__put_character_sequence(__os, __str.data(), __str.size());
1058}
1059
1060template<class _CharT, class _Traits>
1061basic_ostream<_CharT, _Traits>&
1062operator<<(basic_ostream<_CharT, _Traits>& __os,
1063           basic_string_view<_CharT, _Traits> __sv)
1064{
1065    return _VSTD::__put_character_sequence(__os, __sv.data(), __sv.size());
1066}
1067
1068template <class _CharT, class _Traits>
1069inline _LIBCPP_INLINE_VISIBILITY
1070basic_ostream<_CharT, _Traits>&
1071operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __ec)
1072{
1073    return __os << __ec.category().name() << ':' << __ec.value();
1074}
1075
1076template<class _CharT, class _Traits, class _Yp>
1077inline _LIBCPP_INLINE_VISIBILITY
1078basic_ostream<_CharT, _Traits>&
1079operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p)
1080{
1081    return __os << __p.get();
1082}
1083
1084template<class _CharT, class _Traits, class _Yp, class _Dp>
1085inline _LIBCPP_INLINE_VISIBILITY
1086typename enable_if
1087<
1088    is_same<void, typename __void_t<decltype((declval<basic_ostream<_CharT, _Traits>&>() << declval<typename unique_ptr<_Yp, _Dp>::pointer>()))>::type>::value,
1089    basic_ostream<_CharT, _Traits>&
1090>::type
1091operator<<(basic_ostream<_CharT, _Traits>& __os, unique_ptr<_Yp, _Dp> const& __p)
1092{
1093    return __os << __p.get();
1094}
1095
1096template <class _CharT, class _Traits, size_t _Size>
1097basic_ostream<_CharT, _Traits>&
1098operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x)
1099{
1100    return __os << __x.template to_string<_CharT, _Traits>
1101                        (use_facet<ctype<_CharT> >(__os.getloc()).widen('0'),
1102                         use_facet<ctype<_CharT> >(__os.getloc()).widen('1'));
1103}
1104
1105extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<char>;
1106#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1107extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<wchar_t>;
1108#endif
1109
1110_LIBCPP_END_NAMESPACE_STD
1111
1112#endif // _LIBCPP_OSTREAM
1113