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