2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_OSTREAM
11 #define _LIBCPP_OSTREAM
16 template <class charT, class traits = char_traits<charT> >
18 : virtual public basic_ios<charT,traits>
21 // types (inherited from basic_ios (27.5.4)):
22 typedef charT char_type;
23 typedef traits traits_type;
24 typedef typename traits_type::int_type int_type;
25 typedef typename traits_type::pos_type pos_type;
26 typedef typename traits_type::off_type off_type;
28 // 27.7.2.2 Constructor/destructor:
29 explicit basic_ostream(basic_streambuf<char_type,traits>* sb);
30 basic_ostream(basic_ostream&& rhs);
31 virtual ~basic_ostream();
33 // 27.7.2.3 Assign/swap
34 basic_ostream& operator=(const basic_ostream& rhs) = delete; // C++14
35 basic_ostream& operator=(basic_ostream&& rhs);
36 void swap(basic_ostream& rhs);
38 // 27.7.2.4 Prefix/suffix:
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);
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();
69 basic_ostream& seekp(pos_type);
70 basic_ostream& seekp(off_type, ios_base::seekdir);
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);
80 // 27.7.2.6.4 character inserters
82 template<class charT, class traits>
83 basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, charT);
85 template<class charT, class traits>
86 basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, char);
88 template<class traits>
89 basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, char);
91 // signed and unsigned
93 template<class traits>
94 basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, signed char);
96 template<class traits>
97 basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, unsigned char);
100 template<class charT, class traits>
101 basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, const charT*);
103 template<class charT, class traits>
104 basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, const char*);
106 template<class traits>
107 basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, const char*);
109 // signed and unsigned
110 template<class traits>
111 basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, const signed char*);
113 template<class traits>
114 basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, const unsigned char*);
117 template <class charT, class traits>
118 void swap(basic_ostream<charT, traits>& x, basic_ostream<charT, traits>& y);
120 template <class charT, class traits>
121 basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);
123 template <class charT, class traits>
124 basic_ostream<charT,traits>& ends(basic_ostream<charT,traits>& os);
126 template <class charT, class traits>
127 basic_ostream<charT,traits>& flush(basic_ostream<charT,traits>& os);
129 // rvalue stream insertion
130 template <class Stream, class T>
131 Stream&& operator<<(Stream&& os, const T& x);
145 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
146 #pragma GCC system_header
149 _LIBCPP_BEGIN_NAMESPACE_STD
151 template <class _CharT, class _Traits>
152 class _LIBCPP_TEMPLATE_VIS basic_ostream
153 : virtual public basic_ios<_CharT, _Traits>
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;
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();
169 inline _LIBCPP_INLINE_VISIBILITY
170 basic_ostream(basic_ostream&& __rhs);
172 // 27.7.2.3 Assign/swap
173 inline _LIBCPP_INLINE_VISIBILITY
174 basic_ostream& operator=(basic_ostream&& __rhs);
176 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
177 void swap(basic_ostream& __rhs)
178 { basic_ios<char_type, traits_type>::swap(__rhs); }
180 basic_ostream (const basic_ostream& __rhs) = delete;
181 basic_ostream& operator=(const basic_ostream& __rhs) = delete;
184 // 27.7.2.4 Prefix/suffix:
185 class _LIBCPP_TEMPLATE_VIS sentry;
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); }
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; }
197 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
198 basic_ostream& operator<<(ios_base& (*__pf)(ios_base&))
199 { __pf(*this); return *this; }
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);
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));
222 basic_ostream& operator<<(basic_streambuf<char_type, traits_type>* __sb);
224 _LIBCPP_INLINE_VISIBILITY
225 basic_ostream& operator<<(nullptr_t)
226 { return *this << "nullptr"; }
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();
234 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
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);
242 _LIBCPP_INLINE_VISIBILITY
243 basic_ostream() {} // extension, intentially does not initialize
246 template <class _CharT, class _Traits>
247 class _LIBCPP_TEMPLATE_VIS basic_ostream<_CharT, _Traits>::sentry
250 basic_ostream<_CharT, _Traits>& __os_;
253 explicit sentry(basic_ostream<_CharT, _Traits>& __os);
255 sentry(const sentry&) = delete;
256 sentry& operator=(const sentry&) = delete;
258 _LIBCPP_INLINE_VISIBILITY
259 explicit operator bool() const {return __ok_;}
262 template <class _CharT, class _Traits>
263 basic_ostream<_CharT, _Traits>::sentry::sentry(basic_ostream<_CharT, _Traits>& __os)
275 template <class _CharT, class _Traits>
276 basic_ostream<_CharT, _Traits>::sentry::~sentry()
278 if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf)
279 && !uncaught_exception())
281 #ifndef _LIBCPP_NO_EXCEPTIONS
284 #endif // _LIBCPP_NO_EXCEPTIONS
285 if (__os_.rdbuf()->pubsync() == -1)
286 __os_.setstate(ios_base::badbit);
287 #ifndef _LIBCPP_NO_EXCEPTIONS
292 #endif // _LIBCPP_NO_EXCEPTIONS
296 template <class _CharT, class _Traits>
297 basic_ostream<_CharT, _Traits>::basic_ostream(basic_ostream&& __rhs)
302 template <class _CharT, class _Traits>
303 basic_ostream<_CharT, _Traits>&
304 basic_ostream<_CharT, _Traits>::operator=(basic_ostream&& __rhs)
310 template <class _CharT, class _Traits>
311 basic_ostream<_CharT, _Traits>::~basic_ostream()
315 template <class _CharT, class _Traits>
316 basic_ostream<_CharT, _Traits>&
317 basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<char_type, traits_type>* __sb)
319 #ifndef _LIBCPP_NO_EXCEPTIONS
322 #endif // _LIBCPP_NO_EXCEPTIONS
328 #ifndef _LIBCPP_NO_EXCEPTIONS
331 #endif // _LIBCPP_NO_EXCEPTIONS
332 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
333 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
338 for (; __i != __eof; ++__i, ++__o, ++__c)
345 this->setstate(ios_base::failbit);
346 #ifndef _LIBCPP_NO_EXCEPTIONS
350 this->__set_failbit_and_consider_rethrow();
352 #endif // _LIBCPP_NO_EXCEPTIONS
355 this->setstate(ios_base::badbit);
357 #ifndef _LIBCPP_NO_EXCEPTIONS
361 this->__set_badbit_and_consider_rethrow();
363 #endif // _LIBCPP_NO_EXCEPTIONS
367 template <class _CharT, class _Traits>
368 basic_ostream<_CharT, _Traits>&
369 basic_ostream<_CharT, _Traits>::operator<<(bool __n)
371 #ifndef _LIBCPP_NO_EXCEPTIONS
374 #endif // _LIBCPP_NO_EXCEPTIONS
378 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
379 const _Fp& __f = use_facet<_Fp>(this->getloc());
380 if (__f.put(*this, *this, this->fill(), __n).failed())
381 this->setstate(ios_base::badbit | ios_base::failbit);
383 #ifndef _LIBCPP_NO_EXCEPTIONS
387 this->__set_badbit_and_consider_rethrow();
389 #endif // _LIBCPP_NO_EXCEPTIONS
393 template <class _CharT, class _Traits>
394 basic_ostream<_CharT, _Traits>&
395 basic_ostream<_CharT, _Traits>::operator<<(short __n)
397 #ifndef _LIBCPP_NO_EXCEPTIONS
400 #endif // _LIBCPP_NO_EXCEPTIONS
404 ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
405 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
406 const _Fp& __f = use_facet<_Fp>(this->getloc());
407 if (__f.put(*this, *this, this->fill(),
408 __flags == ios_base::oct || __flags == ios_base::hex ?
409 static_cast<long>(static_cast<unsigned short>(__n)) :
410 static_cast<long>(__n)).failed())
411 this->setstate(ios_base::badbit | ios_base::failbit);
413 #ifndef _LIBCPP_NO_EXCEPTIONS
417 this->__set_badbit_and_consider_rethrow();
419 #endif // _LIBCPP_NO_EXCEPTIONS
423 template <class _CharT, class _Traits>
424 basic_ostream<_CharT, _Traits>&
425 basic_ostream<_CharT, _Traits>::operator<<(unsigned short __n)
427 #ifndef _LIBCPP_NO_EXCEPTIONS
430 #endif // _LIBCPP_NO_EXCEPTIONS
434 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
435 const _Fp& __f = use_facet<_Fp>(this->getloc());
436 if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
437 this->setstate(ios_base::badbit | ios_base::failbit);
439 #ifndef _LIBCPP_NO_EXCEPTIONS
443 this->__set_badbit_and_consider_rethrow();
445 #endif // _LIBCPP_NO_EXCEPTIONS
449 template <class _CharT, class _Traits>
450 basic_ostream<_CharT, _Traits>&
451 basic_ostream<_CharT, _Traits>::operator<<(int __n)
453 #ifndef _LIBCPP_NO_EXCEPTIONS
456 #endif // _LIBCPP_NO_EXCEPTIONS
460 ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
461 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
462 const _Fp& __f = use_facet<_Fp>(this->getloc());
463 if (__f.put(*this, *this, this->fill(),
464 __flags == ios_base::oct || __flags == ios_base::hex ?
465 static_cast<long>(static_cast<unsigned int>(__n)) :
466 static_cast<long>(__n)).failed())
467 this->setstate(ios_base::badbit | ios_base::failbit);
469 #ifndef _LIBCPP_NO_EXCEPTIONS
473 this->__set_badbit_and_consider_rethrow();
475 #endif // _LIBCPP_NO_EXCEPTIONS
479 template <class _CharT, class _Traits>
480 basic_ostream<_CharT, _Traits>&
481 basic_ostream<_CharT, _Traits>::operator<<(unsigned int __n)
483 #ifndef _LIBCPP_NO_EXCEPTIONS
486 #endif // _LIBCPP_NO_EXCEPTIONS
490 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
491 const _Fp& __f = use_facet<_Fp>(this->getloc());
492 if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
493 this->setstate(ios_base::badbit | ios_base::failbit);
495 #ifndef _LIBCPP_NO_EXCEPTIONS
499 this->__set_badbit_and_consider_rethrow();
501 #endif // _LIBCPP_NO_EXCEPTIONS
505 template <class _CharT, class _Traits>
506 basic_ostream<_CharT, _Traits>&
507 basic_ostream<_CharT, _Traits>::operator<<(long __n)
509 #ifndef _LIBCPP_NO_EXCEPTIONS
512 #endif // _LIBCPP_NO_EXCEPTIONS
516 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
517 const _Fp& __f = use_facet<_Fp>(this->getloc());
518 if (__f.put(*this, *this, this->fill(), __n).failed())
519 this->setstate(ios_base::badbit | ios_base::failbit);
521 #ifndef _LIBCPP_NO_EXCEPTIONS
525 this->__set_badbit_and_consider_rethrow();
527 #endif // _LIBCPP_NO_EXCEPTIONS
531 template <class _CharT, class _Traits>
532 basic_ostream<_CharT, _Traits>&
533 basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n)
535 #ifndef _LIBCPP_NO_EXCEPTIONS
538 #endif // _LIBCPP_NO_EXCEPTIONS
542 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
543 const _Fp& __f = use_facet<_Fp>(this->getloc());
544 if (__f.put(*this, *this, this->fill(), __n).failed())
545 this->setstate(ios_base::badbit | ios_base::failbit);
547 #ifndef _LIBCPP_NO_EXCEPTIONS
551 this->__set_badbit_and_consider_rethrow();
553 #endif // _LIBCPP_NO_EXCEPTIONS
557 template <class _CharT, class _Traits>
558 basic_ostream<_CharT, _Traits>&
559 basic_ostream<_CharT, _Traits>::operator<<(long long __n)
561 #ifndef _LIBCPP_NO_EXCEPTIONS
564 #endif // _LIBCPP_NO_EXCEPTIONS
568 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
569 const _Fp& __f = use_facet<_Fp>(this->getloc());
570 if (__f.put(*this, *this, this->fill(), __n).failed())
571 this->setstate(ios_base::badbit | ios_base::failbit);
573 #ifndef _LIBCPP_NO_EXCEPTIONS
577 this->__set_badbit_and_consider_rethrow();
579 #endif // _LIBCPP_NO_EXCEPTIONS
583 template <class _CharT, class _Traits>
584 basic_ostream<_CharT, _Traits>&
585 basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n)
587 #ifndef _LIBCPP_NO_EXCEPTIONS
590 #endif // _LIBCPP_NO_EXCEPTIONS
594 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
595 const _Fp& __f = use_facet<_Fp>(this->getloc());
596 if (__f.put(*this, *this, this->fill(), __n).failed())
597 this->setstate(ios_base::badbit | ios_base::failbit);
599 #ifndef _LIBCPP_NO_EXCEPTIONS
603 this->__set_badbit_and_consider_rethrow();
605 #endif // _LIBCPP_NO_EXCEPTIONS
609 template <class _CharT, class _Traits>
610 basic_ostream<_CharT, _Traits>&
611 basic_ostream<_CharT, _Traits>::operator<<(float __n)
613 #ifndef _LIBCPP_NO_EXCEPTIONS
616 #endif // _LIBCPP_NO_EXCEPTIONS
620 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
621 const _Fp& __f = use_facet<_Fp>(this->getloc());
622 if (__f.put(*this, *this, this->fill(), static_cast<double>(__n)).failed())
623 this->setstate(ios_base::badbit | ios_base::failbit);
625 #ifndef _LIBCPP_NO_EXCEPTIONS
629 this->__set_badbit_and_consider_rethrow();
631 #endif // _LIBCPP_NO_EXCEPTIONS
635 template <class _CharT, class _Traits>
636 basic_ostream<_CharT, _Traits>&
637 basic_ostream<_CharT, _Traits>::operator<<(double __n)
639 #ifndef _LIBCPP_NO_EXCEPTIONS
642 #endif // _LIBCPP_NO_EXCEPTIONS
646 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
647 const _Fp& __f = use_facet<_Fp>(this->getloc());
648 if (__f.put(*this, *this, this->fill(), __n).failed())
649 this->setstate(ios_base::badbit | ios_base::failbit);
651 #ifndef _LIBCPP_NO_EXCEPTIONS
655 this->__set_badbit_and_consider_rethrow();
657 #endif // _LIBCPP_NO_EXCEPTIONS
661 template <class _CharT, class _Traits>
662 basic_ostream<_CharT, _Traits>&
663 basic_ostream<_CharT, _Traits>::operator<<(long double __n)
665 #ifndef _LIBCPP_NO_EXCEPTIONS
668 #endif // _LIBCPP_NO_EXCEPTIONS
672 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
673 const _Fp& __f = use_facet<_Fp>(this->getloc());
674 if (__f.put(*this, *this, this->fill(), __n).failed())
675 this->setstate(ios_base::badbit | ios_base::failbit);
677 #ifndef _LIBCPP_NO_EXCEPTIONS
681 this->__set_badbit_and_consider_rethrow();
683 #endif // _LIBCPP_NO_EXCEPTIONS
687 template <class _CharT, class _Traits>
688 basic_ostream<_CharT, _Traits>&
689 basic_ostream<_CharT, _Traits>::operator<<(const void* __n)
691 #ifndef _LIBCPP_NO_EXCEPTIONS
694 #endif // _LIBCPP_NO_EXCEPTIONS
698 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
699 const _Fp& __f = use_facet<_Fp>(this->getloc());
700 if (__f.put(*this, *this, this->fill(), __n).failed())
701 this->setstate(ios_base::badbit | ios_base::failbit);
703 #ifndef _LIBCPP_NO_EXCEPTIONS
707 this->__set_badbit_and_consider_rethrow();
709 #endif // _LIBCPP_NO_EXCEPTIONS
713 template<class _CharT, class _Traits>
714 basic_ostream<_CharT, _Traits>&
715 __put_character_sequence(basic_ostream<_CharT, _Traits>& __os,
716 const _CharT* __str, size_t __len)
718 #ifndef _LIBCPP_NO_EXCEPTIONS
721 #endif // _LIBCPP_NO_EXCEPTIONS
722 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
725 typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
726 if (__pad_and_output(_Ip(__os),
728 (__os.flags() & ios_base::adjustfield) == ios_base::left ?
733 __os.fill()).failed())
734 __os.setstate(ios_base::badbit | ios_base::failbit);
736 #ifndef _LIBCPP_NO_EXCEPTIONS
740 __os.__set_badbit_and_consider_rethrow();
742 #endif // _LIBCPP_NO_EXCEPTIONS
747 template<class _CharT, class _Traits>
748 basic_ostream<_CharT, _Traits>&
749 operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c)
751 return _VSTD::__put_character_sequence(__os, &__c, 1);
754 template<class _CharT, class _Traits>
755 basic_ostream<_CharT, _Traits>&
756 operator<<(basic_ostream<_CharT, _Traits>& __os, char __cn)
758 #ifndef _LIBCPP_NO_EXCEPTIONS
761 #endif // _LIBCPP_NO_EXCEPTIONS
762 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
765 _CharT __c = __os.widen(__cn);
766 typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
767 if (__pad_and_output(_Ip(__os),
769 (__os.flags() & ios_base::adjustfield) == ios_base::left ?
774 __os.fill()).failed())
775 __os.setstate(ios_base::badbit | ios_base::failbit);
777 #ifndef _LIBCPP_NO_EXCEPTIONS
781 __os.__set_badbit_and_consider_rethrow();
783 #endif // _LIBCPP_NO_EXCEPTIONS
787 template<class _Traits>
788 basic_ostream<char, _Traits>&
789 operator<<(basic_ostream<char, _Traits>& __os, char __c)
791 return _VSTD::__put_character_sequence(__os, &__c, 1);
794 template<class _Traits>
795 basic_ostream<char, _Traits>&
796 operator<<(basic_ostream<char, _Traits>& __os, signed char __c)
798 return _VSTD::__put_character_sequence(__os, (char *) &__c, 1);
801 template<class _Traits>
802 basic_ostream<char, _Traits>&
803 operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c)
805 return _VSTD::__put_character_sequence(__os, (char *) &__c, 1);
808 template<class _CharT, class _Traits>
809 basic_ostream<_CharT, _Traits>&
810 operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str)
812 return _VSTD::__put_character_sequence(__os, __str, _Traits::length(__str));
815 template<class _CharT, class _Traits>
816 basic_ostream<_CharT, _Traits>&
817 operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __strn)
819 #ifndef _LIBCPP_NO_EXCEPTIONS
822 #endif // _LIBCPP_NO_EXCEPTIONS
823 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
826 typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
827 size_t __len = char_traits<char>::length(__strn);
828 const int __bs = 100;
830 _CharT* __wb = __wbb;
831 unique_ptr<_CharT, void(*)(void*)> __h(0, free);
834 __wb = (_CharT*)malloc(__len*sizeof(_CharT));
839 for (_CharT* __p = __wb; *__strn != '\0'; ++__strn, ++__p)
840 *__p = __os.widen(*__strn);
841 if (__pad_and_output(_Ip(__os),
843 (__os.flags() & ios_base::adjustfield) == ios_base::left ?
848 __os.fill()).failed())
849 __os.setstate(ios_base::badbit | ios_base::failbit);
851 #ifndef _LIBCPP_NO_EXCEPTIONS
855 __os.__set_badbit_and_consider_rethrow();
857 #endif // _LIBCPP_NO_EXCEPTIONS
861 template<class _Traits>
862 basic_ostream<char, _Traits>&
863 operator<<(basic_ostream<char, _Traits>& __os, const char* __str)
865 return _VSTD::__put_character_sequence(__os, __str, _Traits::length(__str));
868 template<class _Traits>
869 basic_ostream<char, _Traits>&
870 operator<<(basic_ostream<char, _Traits>& __os, const signed char* __str)
872 const char *__s = (const char *) __str;
873 return _VSTD::__put_character_sequence(__os, __s, _Traits::length(__s));
876 template<class _Traits>
877 basic_ostream<char, _Traits>&
878 operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __str)
880 const char *__s = (const char *) __str;
881 return _VSTD::__put_character_sequence(__os, __s, _Traits::length(__s));
884 template <class _CharT, class _Traits>
885 basic_ostream<_CharT, _Traits>&
886 basic_ostream<_CharT, _Traits>::put(char_type __c)
888 #ifndef _LIBCPP_NO_EXCEPTIONS
891 #endif // _LIBCPP_NO_EXCEPTIONS
895 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
899 this->setstate(ios_base::badbit);
901 #ifndef _LIBCPP_NO_EXCEPTIONS
905 this->__set_badbit_and_consider_rethrow();
907 #endif // _LIBCPP_NO_EXCEPTIONS
911 template <class _CharT, class _Traits>
912 basic_ostream<_CharT, _Traits>&
913 basic_ostream<_CharT, _Traits>::write(const char_type* __s, streamsize __n)
915 #ifndef _LIBCPP_NO_EXCEPTIONS
918 #endif // _LIBCPP_NO_EXCEPTIONS
922 if (this->rdbuf()->sputn(__s, __n) != __n)
923 this->setstate(ios_base::badbit);
925 #ifndef _LIBCPP_NO_EXCEPTIONS
929 this->__set_badbit_and_consider_rethrow();
931 #endif // _LIBCPP_NO_EXCEPTIONS
935 template <class _CharT, class _Traits>
936 basic_ostream<_CharT, _Traits>&
937 basic_ostream<_CharT, _Traits>::flush()
939 #ifndef _LIBCPP_NO_EXCEPTIONS
942 #endif // _LIBCPP_NO_EXCEPTIONS
948 if (this->rdbuf()->pubsync() == -1)
949 this->setstate(ios_base::badbit);
952 #ifndef _LIBCPP_NO_EXCEPTIONS
956 this->__set_badbit_and_consider_rethrow();
958 #endif // _LIBCPP_NO_EXCEPTIONS
962 template <class _CharT, class _Traits>
963 typename basic_ostream<_CharT, _Traits>::pos_type
964 basic_ostream<_CharT, _Traits>::tellp()
968 return this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
971 template <class _CharT, class _Traits>
972 basic_ostream<_CharT, _Traits>&
973 basic_ostream<_CharT, _Traits>::seekp(pos_type __pos)
978 if (this->rdbuf()->pubseekpos(__pos, ios_base::out) == pos_type(-1))
979 this->setstate(ios_base::failbit);
984 template <class _CharT, class _Traits>
985 basic_ostream<_CharT, _Traits>&
986 basic_ostream<_CharT, _Traits>::seekp(off_type __off, ios_base::seekdir __dir)
991 if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::out) == pos_type(-1))
992 this->setstate(ios_base::failbit);
997 template <class _CharT, class _Traits>
999 basic_ostream<_CharT, _Traits>&
1000 endl(basic_ostream<_CharT, _Traits>& __os)
1002 __os.put(__os.widen('\n'));
1007 template <class _CharT, class _Traits>
1009 basic_ostream<_CharT, _Traits>&
1010 ends(basic_ostream<_CharT, _Traits>& __os)
1016 template <class _CharT, class _Traits>
1018 basic_ostream<_CharT, _Traits>&
1019 flush(basic_ostream<_CharT, _Traits>& __os)
1025 template <class _Stream, class _Tp, class = void>
1026 struct __is_ostreamable : false_type { };
1028 template <class _Stream, class _Tp>
1029 struct __is_ostreamable<_Stream, _Tp, decltype(
1030 declval<_Stream>() << declval<_Tp>(), void()
1033 template <class _Stream, class _Tp, class = typename enable_if<
1034 _And<is_base_of<ios_base, _Stream>,
1035 __is_ostreamable<_Stream&, const _Tp&> >::value
1037 _LIBCPP_INLINE_VISIBILITY
1038 _Stream&& operator<<(_Stream&& __os, const _Tp& __x)
1041 return _VSTD::move(__os);
1044 template<class _CharT, class _Traits, class _Allocator>
1045 basic_ostream<_CharT, _Traits>&
1046 operator<<(basic_ostream<_CharT, _Traits>& __os,
1047 const basic_string<_CharT, _Traits, _Allocator>& __str)
1049 return _VSTD::__put_character_sequence(__os, __str.data(), __str.size());
1052 template<class _CharT, class _Traits>
1053 basic_ostream<_CharT, _Traits>&
1054 operator<<(basic_ostream<_CharT, _Traits>& __os,
1055 basic_string_view<_CharT, _Traits> __sv)
1057 return _VSTD::__put_character_sequence(__os, __sv.data(), __sv.size());
1060 template <class _CharT, class _Traits>
1061 inline _LIBCPP_INLINE_VISIBILITY
1062 basic_ostream<_CharT, _Traits>&
1063 operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __ec)
1065 return __os << __ec.category().name() << ':' << __ec.value();
1068 template<class _CharT, class _Traits, class _Yp>
1069 inline _LIBCPP_INLINE_VISIBILITY
1070 basic_ostream<_CharT, _Traits>&
1071 operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p)
1073 return __os << __p.get();
1076 template<class _CharT, class _Traits, class _Yp, class _Dp>
1077 inline _LIBCPP_INLINE_VISIBILITY
1080 is_same<void, typename __void_t<decltype((declval<basic_ostream<_CharT, _Traits>&>() << declval<typename unique_ptr<_Yp, _Dp>::pointer>()))>::type>::value,
1081 basic_ostream<_CharT, _Traits>&
1083 operator<<(basic_ostream<_CharT, _Traits>& __os, unique_ptr<_Yp, _Dp> const& __p)
1085 return __os << __p.get();
1088 template <class _CharT, class _Traits, size_t _Size>
1089 basic_ostream<_CharT, _Traits>&
1090 operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x)
1092 return __os << __x.template to_string<_CharT, _Traits>
1093 (use_facet<ctype<_CharT> >(__os.getloc()).widen('0'),
1094 use_facet<ctype<_CharT> >(__os.getloc()).widen('1'));
1097 _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<char>)
1098 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1099 _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<wchar_t>)
1102 _LIBCPP_END_NAMESPACE_STD
1104 #endif // _LIBCPP_OSTREAM