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