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);
133 template<class traits>
134 basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, wchar_t) = delete; // since C++20
135 template<class traits>
136 basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, char8_t) = delete; // since C++20
137 template<class traits>
138 basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, char16_t) = delete; // since C++20
139 template<class traits>
140 basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, char32_t) = delete; // since C++20
141 template<class traits>
142 basic_ostream<wchar_t, traits>& operator<<(basic_ostream<wchar_t, traits>&, char8_t) = delete; // since C++20
143 template<class traits>
144 basic_ostream<wchar_t, traits>& operator<<(basic_ostream<wchar_t, traits>&, char16_t) = delete; // since C++20
145 template<class traits>
146 basic_ostream<wchar_t, traits>& operator<<(basic_ostream<wchar_t, traits>&, char32_t) = delete; // since C++20
147 template<class traits>
148 basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, const wchar_t*) = delete; // since C++20
149 template<class traits>
150 basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, const char8_t*) = delete; // since C++20
151 template<class traits>
152 basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, const char16_t*) = delete; // since C++20
153 template<class traits>
154 basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, const char32_t*) = delete; // since C++20
155 template<class traits>
156 basic_ostream<wchar_t, traits>& operator<<(basic_ostream<wchar_t, traits>&, const char8_t*) = delete; // since C++20
157 template<class traits>
158 basic_ostream<wchar_t, traits>& operator<<(basic_ostream<wchar_t, traits>&, const char16_t*) = delete; // since C++20
159 template<class traits>
160 basic_ostream<wchar_t, traits>& operator<<(basic_ostream<wchar_t, traits>&, const char32_t*) = delete; // since C++20
166 #include <__assert> // all public C++ headers provide the assertion handler
168 #include <__exception/operations.h>
169 #include <__fwd/ostream.h>
170 #include <__memory/shared_ptr.h>
171 #include <__memory/unique_ptr.h>
172 #include <__system_error/error_code.h>
173 #include <__type_traits/conjunction.h>
174 #include <__type_traits/enable_if.h>
175 #include <__type_traits/is_base_of.h>
176 #include <__type_traits/void_t.h>
177 #include <__utility/declval.h>
185 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
186 # pragma GCC system_header
189 _LIBCPP_BEGIN_NAMESPACE_STD
191 template <class _CharT, class _Traits>
192 class _LIBCPP_TEMPLATE_VIS basic_ostream
193 : virtual public basic_ios<_CharT, _Traits>
196 // types (inherited from basic_ios (27.5.4)):
197 typedef _CharT char_type;
198 typedef _Traits traits_type;
199 typedef typename traits_type::int_type int_type;
200 typedef typename traits_type::pos_type pos_type;
201 typedef typename traits_type::off_type off_type;
203 // 27.7.2.2 Constructor/destructor:
204 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
205 explicit basic_ostream(basic_streambuf<char_type, traits_type>* __sb)
206 { this->init(__sb); }
207 ~basic_ostream() override;
209 inline _LIBCPP_INLINE_VISIBILITY
210 basic_ostream(basic_ostream&& __rhs);
212 // 27.7.2.3 Assign/swap
213 inline _LIBCPP_INLINE_VISIBILITY
214 basic_ostream& operator=(basic_ostream&& __rhs);
216 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
217 void swap(basic_ostream& __rhs)
218 { basic_ios<char_type, traits_type>::swap(__rhs); }
220 basic_ostream (const basic_ostream& __rhs) = delete;
221 basic_ostream& operator=(const basic_ostream& __rhs) = delete;
224 // 27.7.2.4 Prefix/suffix:
225 class _LIBCPP_TEMPLATE_VIS sentry;
227 // 27.7.2.6 Formatted output:
228 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
229 basic_ostream& operator<<(basic_ostream& (*__pf)(basic_ostream&))
230 { return __pf(*this); }
232 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
233 basic_ostream& operator<<(basic_ios<char_type, traits_type>&
234 (*__pf)(basic_ios<char_type,traits_type>&))
235 { __pf(*this); return *this; }
237 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
238 basic_ostream& operator<<(ios_base& (*__pf)(ios_base&))
239 { __pf(*this); return *this; }
241 basic_ostream& operator<<(bool __n);
242 basic_ostream& operator<<(short __n);
243 basic_ostream& operator<<(unsigned short __n);
244 basic_ostream& operator<<(int __n);
245 basic_ostream& operator<<(unsigned int __n);
246 basic_ostream& operator<<(long __n);
247 basic_ostream& operator<<(unsigned long __n);
248 basic_ostream& operator<<(long long __n);
249 basic_ostream& operator<<(unsigned long long __n);
250 basic_ostream& operator<<(float __f);
251 basic_ostream& operator<<(double __f);
252 basic_ostream& operator<<(long double __f);
253 basic_ostream& operator<<(const void* __p);
255 #if _LIBCPP_STD_VER >= 23
256 _LIBCPP_HIDE_FROM_ABI
257 basic_ostream& operator<<(const volatile void* __p) {
258 return operator<<(const_cast<const void*>(__p));
262 basic_ostream& operator<<(basic_streambuf<char_type, traits_type>* __sb);
264 #if _LIBCPP_STD_VER >= 17
265 // LWG 2221 - nullptr. This is not backported to older standards modes.
266 // See https://reviews.llvm.org/D127033 for more info on the rationale.
267 _LIBCPP_INLINE_VISIBILITY
268 basic_ostream& operator<<(nullptr_t)
269 { return *this << "nullptr"; }
272 // 27.7.2.7 Unformatted output:
273 basic_ostream& put(char_type __c);
274 basic_ostream& write(const char_type* __s, streamsize __n);
275 basic_ostream& flush();
278 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
280 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
281 basic_ostream& seekp(pos_type __pos);
282 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
283 basic_ostream& seekp(off_type __off, ios_base::seekdir __dir);
286 _LIBCPP_INLINE_VISIBILITY
287 basic_ostream() {} // extension, intentially does not initialize
290 template <class _CharT, class _Traits>
291 class _LIBCPP_TEMPLATE_VIS basic_ostream<_CharT, _Traits>::sentry
294 basic_ostream<_CharT, _Traits>& __os_;
297 explicit sentry(basic_ostream<_CharT, _Traits>& __os);
299 sentry(const sentry&) = delete;
300 sentry& operator=(const sentry&) = delete;
302 _LIBCPP_INLINE_VISIBILITY
303 explicit operator bool() const {return __ok_;}
306 template <class _CharT, class _Traits>
307 basic_ostream<_CharT, _Traits>::sentry::sentry(basic_ostream<_CharT, _Traits>& __os)
319 template <class _CharT, class _Traits>
320 basic_ostream<_CharT, _Traits>::sentry::~sentry()
322 if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf)
323 && !uncaught_exception())
325 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
328 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
329 if (__os_.rdbuf()->pubsync() == -1)
330 __os_.setstate(ios_base::badbit);
331 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
336 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
340 template <class _CharT, class _Traits>
341 basic_ostream<_CharT, _Traits>::basic_ostream(basic_ostream&& __rhs)
346 template <class _CharT, class _Traits>
347 basic_ostream<_CharT, _Traits>&
348 basic_ostream<_CharT, _Traits>::operator=(basic_ostream&& __rhs)
354 template <class _CharT, class _Traits>
355 basic_ostream<_CharT, _Traits>::~basic_ostream()
359 template <class _CharT, class _Traits>
360 basic_ostream<_CharT, _Traits>&
361 basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<char_type, traits_type>* __sb)
363 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
366 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
372 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
375 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
376 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
377 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
382 for (; __i != __eof; ++__i, ++__o, ++__c)
389 this->setstate(ios_base::failbit);
390 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
394 this->__set_failbit_and_consider_rethrow();
396 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
399 this->setstate(ios_base::badbit);
401 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
405 this->__set_badbit_and_consider_rethrow();
407 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
411 template <class _CharT, class _Traits>
412 basic_ostream<_CharT, _Traits>&
413 basic_ostream<_CharT, _Traits>::operator<<(bool __n)
415 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
418 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
422 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
423 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
424 if (__f.put(*this, *this, this->fill(), __n).failed())
425 this->setstate(ios_base::badbit | ios_base::failbit);
427 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
431 this->__set_badbit_and_consider_rethrow();
433 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
437 template <class _CharT, class _Traits>
438 basic_ostream<_CharT, _Traits>&
439 basic_ostream<_CharT, _Traits>::operator<<(short __n)
441 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
444 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
448 ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
449 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
450 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
451 if (__f.put(*this, *this, this->fill(),
452 __flags == ios_base::oct || __flags == ios_base::hex ?
453 static_cast<long>(static_cast<unsigned short>(__n)) :
454 static_cast<long>(__n)).failed())
455 this->setstate(ios_base::badbit | ios_base::failbit);
457 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
461 this->__set_badbit_and_consider_rethrow();
463 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
467 template <class _CharT, class _Traits>
468 basic_ostream<_CharT, _Traits>&
469 basic_ostream<_CharT, _Traits>::operator<<(unsigned short __n)
471 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
474 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
478 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
479 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
480 if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
481 this->setstate(ios_base::badbit | ios_base::failbit);
483 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
487 this->__set_badbit_and_consider_rethrow();
489 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
493 template <class _CharT, class _Traits>
494 basic_ostream<_CharT, _Traits>&
495 basic_ostream<_CharT, _Traits>::operator<<(int __n)
497 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
500 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
504 ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
505 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
506 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
507 if (__f.put(*this, *this, this->fill(),
508 __flags == ios_base::oct || __flags == ios_base::hex ?
509 static_cast<long>(static_cast<unsigned int>(__n)) :
510 static_cast<long>(__n)).failed())
511 this->setstate(ios_base::badbit | ios_base::failbit);
513 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
517 this->__set_badbit_and_consider_rethrow();
519 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
523 template <class _CharT, class _Traits>
524 basic_ostream<_CharT, _Traits>&
525 basic_ostream<_CharT, _Traits>::operator<<(unsigned int __n)
527 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
530 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
534 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
535 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
536 if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
537 this->setstate(ios_base::badbit | ios_base::failbit);
539 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
543 this->__set_badbit_and_consider_rethrow();
545 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
549 template <class _CharT, class _Traits>
550 basic_ostream<_CharT, _Traits>&
551 basic_ostream<_CharT, _Traits>::operator<<(long __n)
553 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
556 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
560 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
561 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
562 if (__f.put(*this, *this, this->fill(), __n).failed())
563 this->setstate(ios_base::badbit | ios_base::failbit);
565 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
569 this->__set_badbit_and_consider_rethrow();
571 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
575 template <class _CharT, class _Traits>
576 basic_ostream<_CharT, _Traits>&
577 basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n)
579 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
582 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
586 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
587 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
588 if (__f.put(*this, *this, this->fill(), __n).failed())
589 this->setstate(ios_base::badbit | ios_base::failbit);
591 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
595 this->__set_badbit_and_consider_rethrow();
597 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
601 template <class _CharT, class _Traits>
602 basic_ostream<_CharT, _Traits>&
603 basic_ostream<_CharT, _Traits>::operator<<(long long __n)
605 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
608 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
612 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
613 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
614 if (__f.put(*this, *this, this->fill(), __n).failed())
615 this->setstate(ios_base::badbit | ios_base::failbit);
617 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
621 this->__set_badbit_and_consider_rethrow();
623 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
627 template <class _CharT, class _Traits>
628 basic_ostream<_CharT, _Traits>&
629 basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n)
631 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
634 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
638 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
639 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
640 if (__f.put(*this, *this, this->fill(), __n).failed())
641 this->setstate(ios_base::badbit | ios_base::failbit);
643 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
647 this->__set_badbit_and_consider_rethrow();
649 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
653 template <class _CharT, class _Traits>
654 basic_ostream<_CharT, _Traits>&
655 basic_ostream<_CharT, _Traits>::operator<<(float __n)
657 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
660 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
664 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
665 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
666 if (__f.put(*this, *this, this->fill(), static_cast<double>(__n)).failed())
667 this->setstate(ios_base::badbit | ios_base::failbit);
669 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
673 this->__set_badbit_and_consider_rethrow();
675 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
679 template <class _CharT, class _Traits>
680 basic_ostream<_CharT, _Traits>&
681 basic_ostream<_CharT, _Traits>::operator<<(double __n)
683 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
686 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
690 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
691 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
692 if (__f.put(*this, *this, this->fill(), __n).failed())
693 this->setstate(ios_base::badbit | ios_base::failbit);
695 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
699 this->__set_badbit_and_consider_rethrow();
701 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
705 template <class _CharT, class _Traits>
706 basic_ostream<_CharT, _Traits>&
707 basic_ostream<_CharT, _Traits>::operator<<(long double __n)
709 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
712 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
716 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
717 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
718 if (__f.put(*this, *this, this->fill(), __n).failed())
719 this->setstate(ios_base::badbit | ios_base::failbit);
721 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
725 this->__set_badbit_and_consider_rethrow();
727 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
731 template <class _CharT, class _Traits>
732 basic_ostream<_CharT, _Traits>&
733 basic_ostream<_CharT, _Traits>::operator<<(const void* __n)
735 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
738 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
742 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
743 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
744 if (__f.put(*this, *this, this->fill(), __n).failed())
745 this->setstate(ios_base::badbit | ios_base::failbit);
747 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
751 this->__set_badbit_and_consider_rethrow();
753 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
757 template<class _CharT, class _Traits>
758 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
759 __put_character_sequence(basic_ostream<_CharT, _Traits>& __os,
760 const _CharT* __str, size_t __len)
762 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
765 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
766 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
769 typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
770 if (std::__pad_and_output(_Ip(__os),
772 (__os.flags() & ios_base::adjustfield) == ios_base::left ?
777 __os.fill()).failed())
778 __os.setstate(ios_base::badbit | ios_base::failbit);
780 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
784 __os.__set_badbit_and_consider_rethrow();
786 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
791 template<class _CharT, class _Traits>
792 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
793 operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c)
795 return _VSTD::__put_character_sequence(__os, &__c, 1);
798 template<class _CharT, class _Traits>
799 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
800 operator<<(basic_ostream<_CharT, _Traits>& __os, char __cn)
802 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
805 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
806 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
809 _CharT __c = __os.widen(__cn);
810 typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
811 if (std::__pad_and_output(_Ip(__os),
813 (__os.flags() & ios_base::adjustfield) == ios_base::left ?
818 __os.fill()).failed())
819 __os.setstate(ios_base::badbit | ios_base::failbit);
821 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
825 __os.__set_badbit_and_consider_rethrow();
827 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
831 template<class _Traits>
832 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>&
833 operator<<(basic_ostream<char, _Traits>& __os, char __c)
835 return _VSTD::__put_character_sequence(__os, &__c, 1);
838 template<class _Traits>
839 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>&
840 operator<<(basic_ostream<char, _Traits>& __os, signed char __c)
842 return _VSTD::__put_character_sequence(__os, (char *) &__c, 1);
845 template<class _Traits>
846 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>&
847 operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c)
849 return _VSTD::__put_character_sequence(__os, (char *) &__c, 1);
852 template<class _CharT, class _Traits>
853 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
854 operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str)
856 return _VSTD::__put_character_sequence(__os, __str, _Traits::length(__str));
859 template<class _CharT, class _Traits>
860 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
861 operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __strn)
863 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
866 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
867 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
870 typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
871 size_t __len = char_traits<char>::length(__strn);
872 const int __bs = 100;
874 _CharT* __wb = __wbb;
875 unique_ptr<_CharT, void(*)(void*)> __h(0, free);
878 __wb = (_CharT*)malloc(__len*sizeof(_CharT));
883 for (_CharT* __p = __wb; *__strn != '\0'; ++__strn, ++__p)
884 *__p = __os.widen(*__strn);
885 if (std::__pad_and_output(_Ip(__os),
887 (__os.flags() & ios_base::adjustfield) == ios_base::left ?
892 __os.fill()).failed())
893 __os.setstate(ios_base::badbit | ios_base::failbit);
895 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
899 __os.__set_badbit_and_consider_rethrow();
901 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
905 template<class _Traits>
906 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>&
907 operator<<(basic_ostream<char, _Traits>& __os, const char* __str)
909 return _VSTD::__put_character_sequence(__os, __str, _Traits::length(__str));
912 template<class _Traits>
913 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>&
914 operator<<(basic_ostream<char, _Traits>& __os, const signed char* __str)
916 const char *__s = (const char *) __str;
917 return _VSTD::__put_character_sequence(__os, __s, _Traits::length(__s));
920 template<class _Traits>
921 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>&
922 operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __str)
924 const char *__s = (const char *) __str;
925 return _VSTD::__put_character_sequence(__os, __s, _Traits::length(__s));
928 template <class _CharT, class _Traits>
929 basic_ostream<_CharT, _Traits>&
930 basic_ostream<_CharT, _Traits>::put(char_type __c)
932 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
935 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
939 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
943 this->setstate(ios_base::badbit);
945 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
949 this->__set_badbit_and_consider_rethrow();
951 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
955 template <class _CharT, class _Traits>
956 basic_ostream<_CharT, _Traits>&
957 basic_ostream<_CharT, _Traits>::write(const char_type* __s, streamsize __n)
959 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
962 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
966 if (this->rdbuf()->sputn(__s, __n) != __n)
967 this->setstate(ios_base::badbit);
969 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
973 this->__set_badbit_and_consider_rethrow();
975 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
979 template <class _CharT, class _Traits>
980 basic_ostream<_CharT, _Traits>&
981 basic_ostream<_CharT, _Traits>::flush()
983 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
986 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
992 if (this->rdbuf()->pubsync() == -1)
993 this->setstate(ios_base::badbit);
996 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1000 this->__set_badbit_and_consider_rethrow();
1002 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1006 template <class _CharT, class _Traits>
1007 typename basic_ostream<_CharT, _Traits>::pos_type
1008 basic_ostream<_CharT, _Traits>::tellp()
1011 return pos_type(-1);
1012 return this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
1015 template <class _CharT, class _Traits>
1016 basic_ostream<_CharT, _Traits>&
1017 basic_ostream<_CharT, _Traits>::seekp(pos_type __pos)
1022 if (this->rdbuf()->pubseekpos(__pos, ios_base::out) == pos_type(-1))
1023 this->setstate(ios_base::failbit);
1028 template <class _CharT, class _Traits>
1029 basic_ostream<_CharT, _Traits>&
1030 basic_ostream<_CharT, _Traits>::seekp(off_type __off, ios_base::seekdir __dir)
1035 if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::out) == pos_type(-1))
1036 this->setstate(ios_base::failbit);
1041 template <class _CharT, class _Traits>
1042 _LIBCPP_HIDE_FROM_ABI inline
1043 basic_ostream<_CharT, _Traits>&
1044 endl(basic_ostream<_CharT, _Traits>& __os)
1046 __os.put(__os.widen('\n'));
1051 template <class _CharT, class _Traits>
1052 _LIBCPP_HIDE_FROM_ABI inline
1053 basic_ostream<_CharT, _Traits>&
1054 ends(basic_ostream<_CharT, _Traits>& __os)
1060 template <class _CharT, class _Traits>
1061 _LIBCPP_HIDE_FROM_ABI inline
1062 basic_ostream<_CharT, _Traits>&
1063 flush(basic_ostream<_CharT, _Traits>& __os)
1069 template <class _Stream, class _Tp, class = void>
1070 struct __is_ostreamable : false_type { };
1072 template <class _Stream, class _Tp>
1073 struct __is_ostreamable<_Stream, _Tp, decltype(
1074 std::declval<_Stream>() << std::declval<_Tp>(), void()
1077 template <class _Stream,
1079 __enable_if_t<_And<is_base_of<ios_base, _Stream>, __is_ostreamable<_Stream&, const _Tp&> >::value, int> = 0>
1080 _LIBCPP_INLINE_VISIBILITY
1081 _Stream&& operator<<(_Stream&& __os, const _Tp& __x)
1084 return _VSTD::move(__os);
1087 template<class _CharT, class _Traits, class _Allocator>
1088 basic_ostream<_CharT, _Traits>&
1089 operator<<(basic_ostream<_CharT, _Traits>& __os,
1090 const basic_string<_CharT, _Traits, _Allocator>& __str)
1092 return _VSTD::__put_character_sequence(__os, __str.data(), __str.size());
1095 template<class _CharT, class _Traits>
1096 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
1097 operator<<(basic_ostream<_CharT, _Traits>& __os,
1098 basic_string_view<_CharT, _Traits> __sv)
1100 return _VSTD::__put_character_sequence(__os, __sv.data(), __sv.size());
1103 template <class _CharT, class _Traits>
1104 inline _LIBCPP_INLINE_VISIBILITY
1105 basic_ostream<_CharT, _Traits>&
1106 operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __ec)
1108 return __os << __ec.category().name() << ':' << __ec.value();
1111 template<class _CharT, class _Traits, class _Yp>
1112 inline _LIBCPP_INLINE_VISIBILITY
1113 basic_ostream<_CharT, _Traits>&
1114 operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p)
1116 return __os << __p.get();
1124 __enable_if_t<is_same<void,
1125 __void_t<decltype((std::declval<basic_ostream<_CharT, _Traits>&>()
1126 << std::declval<typename unique_ptr<_Yp, _Dp>::pointer>()))> >::value,
1128 inline _LIBCPP_INLINE_VISIBILITY basic_ostream<_CharT, _Traits>&
1129 operator<<(basic_ostream<_CharT, _Traits>& __os, unique_ptr<_Yp, _Dp> const& __p) {
1130 return __os << __p.get();
1133 template <class _CharT, class _Traits, size_t _Size>
1134 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
1135 operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x)
1137 return __os << __x.template to_string<_CharT, _Traits>
1138 (std::use_facet<ctype<_CharT> >(__os.getloc()).widen('0'),
1139 std::use_facet<ctype<_CharT> >(__os.getloc()).widen('1'));
1142 #if _LIBCPP_STD_VER >= 20
1144 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1145 template <class _Traits>
1146 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, wchar_t) = delete;
1148 template <class _Traits>
1149 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const wchar_t*) = delete;
1151 template <class _Traits>
1152 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char16_t) = delete;
1154 template <class _Traits>
1155 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char32_t) = delete;
1157 template <class _Traits>
1158 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char16_t*) = delete;
1160 template <class _Traits>
1161 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char32_t*) = delete;
1163 #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
1165 #ifndef _LIBCPP_HAS_NO_CHAR8_T
1166 template <class _Traits>
1167 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char8_t) = delete;
1169 template <class _Traits>
1170 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char8_t) = delete;
1172 template <class _Traits>
1173 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char8_t*) = delete;
1175 template <class _Traits>
1176 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char8_t*) = delete;
1179 template <class _Traits>
1180 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char16_t) = delete;
1182 template <class _Traits>
1183 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char32_t) = delete;
1185 template <class _Traits>
1186 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char16_t*) = delete;
1188 template <class _Traits>
1189 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char32_t*) = delete;
1191 #endif // _LIBCPP_STD_VER >= 20
1193 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<char>;
1194 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1195 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<wchar_t>;
1198 _LIBCPP_END_NAMESPACE_STD
1200 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1202 # include <concepts>
1205 # include <iterator>
1206 # include <stdexcept>
1207 # include <type_traits>
1210 #endif // _LIBCPP_OSTREAM