1 //===---------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===---------------------------------------------------------------------===//
9 #ifndef _LIBCPP___OSTREAM_BASIC_OSTREAM_H
10 #define _LIBCPP___OSTREAM_BASIC_OSTREAM_H
14 #if _LIBCPP_HAS_LOCALIZATION
16 # include <__exception/operations.h>
17 # include <__memory/shared_ptr.h>
18 # include <__memory/unique_ptr.h>
19 # include <__ostream/put_character_sequence.h>
20 # include <__system_error/error_code.h>
21 # include <__type_traits/conjunction.h>
22 # include <__type_traits/enable_if.h>
23 # include <__type_traits/is_base_of.h>
24 # include <__type_traits/void_t.h>
25 # include <__utility/declval.h>
29 # include <new> // for __throw_bad_alloc
31 # include <string_view>
33 # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
34 # pragma GCC system_header
38 # include <__undef_macros>
40 _LIBCPP_BEGIN_NAMESPACE_STD
42 template <class _CharT
, class _Traits
>
43 class _LIBCPP_TEMPLATE_VIS basic_ostream
: virtual public basic_ios
<_CharT
, _Traits
> {
45 // types (inherited from basic_ios (27.5.4)):
46 typedef _CharT char_type
;
47 typedef _Traits traits_type
;
48 typedef typename
traits_type::int_type int_type
;
49 typedef typename
traits_type::pos_type pos_type
;
50 typedef typename
traits_type::off_type off_type
;
52 // 27.7.2.2 Constructor/destructor:
53 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
explicit basic_ostream(basic_streambuf
<char_type
, traits_type
>* __sb
) {
56 ~basic_ostream() override
;
58 basic_ostream(const basic_ostream
& __rhs
) = delete;
59 basic_ostream
& operator=(const basic_ostream
& __rhs
) = delete;
62 inline _LIBCPP_HIDE_FROM_ABI
basic_ostream(basic_ostream
&& __rhs
);
64 // 27.7.2.3 Assign/swap
65 inline _LIBCPP_HIDE_FROM_ABI basic_ostream
& operator=(basic_ostream
&& __rhs
);
67 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
void swap(basic_ostream
& __rhs
) {
68 basic_ios
<char_type
, traits_type
>::swap(__rhs
);
72 // 27.7.2.4 Prefix/suffix:
73 class _LIBCPP_TEMPLATE_VIS sentry
;
75 // 27.7.2.6 Formatted output:
76 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream
& operator<<(basic_ostream
& (*__pf
)(basic_ostream
&)) {
80 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream
&
81 operator<<(basic_ios
<char_type
, traits_type
>& (*__pf
)(basic_ios
<char_type
, traits_type
>&)) {
86 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream
& operator<<(ios_base
& (*__pf
)(ios_base
&)) {
91 basic_ostream
& operator<<(bool __n
);
92 basic_ostream
& operator<<(short __n
);
93 basic_ostream
& operator<<(unsigned short __n
);
94 basic_ostream
& operator<<(int __n
);
95 basic_ostream
& operator<<(unsigned int __n
);
96 basic_ostream
& operator<<(long __n
);
97 basic_ostream
& operator<<(unsigned long __n
);
98 basic_ostream
& operator<<(long long __n
);
99 basic_ostream
& operator<<(unsigned long long __n
);
100 basic_ostream
& operator<<(float __f
);
101 basic_ostream
& operator<<(double __f
);
102 basic_ostream
& operator<<(long double __f
);
103 basic_ostream
& operator<<(const void* __p
);
105 # if _LIBCPP_STD_VER >= 23
106 _LIBCPP_HIDE_FROM_ABI basic_ostream
& operator<<(const volatile void* __p
) {
107 return operator<<(const_cast<const void*>(__p
));
111 basic_ostream
& operator<<(basic_streambuf
<char_type
, traits_type
>* __sb
);
113 # if _LIBCPP_STD_VER >= 17
114 // LWG 2221 - nullptr. This is not backported to older standards modes.
115 // See https://reviews.llvm.org/D127033 for more info on the rationale.
116 _LIBCPP_HIDE_FROM_ABI basic_ostream
& operator<<(nullptr_t
) { return *this << "nullptr"; }
119 // 27.7.2.7 Unformatted output:
120 basic_ostream
& put(char_type __c
);
121 basic_ostream
& write(const char_type
* __s
, streamsize __n
);
122 basic_ostream
& flush();
125 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 pos_type
tellp();
126 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream
& seekp(pos_type __pos
);
127 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream
& seekp(off_type __off
, ios_base::seekdir __dir
);
130 _LIBCPP_HIDE_FROM_ABI
basic_ostream() {} // extension, intentially does not initialize
133 template <class _CharT
, class _Traits
>
134 class _LIBCPP_TEMPLATE_VIS basic_ostream
<_CharT
, _Traits
>::sentry
{
136 basic_ostream
<_CharT
, _Traits
>& __os_
;
139 explicit sentry(basic_ostream
<_CharT
, _Traits
>& __os
);
141 sentry(const sentry
&) = delete;
142 sentry
& operator=(const sentry
&) = delete;
144 _LIBCPP_HIDE_FROM_ABI
explicit operator bool() const { return __ok_
; }
147 template <class _CharT
, class _Traits
>
148 basic_ostream
<_CharT
, _Traits
>::sentry::sentry(basic_ostream
<_CharT
, _Traits
>& __os
) : __ok_(false), __os_(__os
) {
156 template <class _CharT
, class _Traits
>
157 basic_ostream
<_CharT
, _Traits
>::sentry::~sentry() {
158 if (__os_
.rdbuf() && __os_
.good() && (__os_
.flags() & ios_base::unitbuf
) && uncaught_exceptions() == 0) {
159 # if _LIBCPP_HAS_EXCEPTIONS
161 # endif // _LIBCPP_HAS_EXCEPTIONS
162 if (__os_
.rdbuf()->pubsync() == -1)
163 __os_
.setstate(ios_base::badbit
);
164 # if _LIBCPP_HAS_EXCEPTIONS
167 # endif // _LIBCPP_HAS_EXCEPTIONS
171 template <class _CharT
, class _Traits
>
172 basic_ostream
<_CharT
, _Traits
>::basic_ostream(basic_ostream
&& __rhs
) {
176 template <class _CharT
, class _Traits
>
177 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator=(basic_ostream
&& __rhs
) {
182 template <class _CharT
, class _Traits
>
183 basic_ostream
<_CharT
, _Traits
>::~basic_ostream() {}
185 template <class _CharT
, class _Traits
>
186 basic_ostream
<_CharT
, _Traits
>&
187 basic_ostream
<_CharT
, _Traits
>::operator<<(basic_streambuf
<char_type
, traits_type
>* __sb
) {
188 # if _LIBCPP_HAS_EXCEPTIONS
190 # endif // _LIBCPP_HAS_EXCEPTIONS
194 # if _LIBCPP_HAS_EXCEPTIONS
196 # endif // _LIBCPP_HAS_EXCEPTIONS
197 typedef istreambuf_iterator
<_CharT
, _Traits
> _Ip
;
198 typedef ostreambuf_iterator
<_CharT
, _Traits
> _Op
;
203 for (; __i
!= __eof
; ++__i
, ++__o
, ++__c
) {
209 this->setstate(ios_base::failbit
);
210 # if _LIBCPP_HAS_EXCEPTIONS
212 this->__set_failbit_and_consider_rethrow();
214 # endif // _LIBCPP_HAS_EXCEPTIONS
216 this->setstate(ios_base::badbit
);
218 # if _LIBCPP_HAS_EXCEPTIONS
220 this->__set_badbit_and_consider_rethrow();
222 # endif // _LIBCPP_HAS_EXCEPTIONS
226 template <class _CharT
, class _Traits
>
227 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(bool __n
) {
228 # if _LIBCPP_HAS_EXCEPTIONS
230 # endif // _LIBCPP_HAS_EXCEPTIONS
233 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
234 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
235 if (__f
.put(*this, *this, this->fill(), __n
).failed())
236 this->setstate(ios_base::badbit
| ios_base::failbit
);
238 # if _LIBCPP_HAS_EXCEPTIONS
240 this->__set_badbit_and_consider_rethrow();
242 # endif // _LIBCPP_HAS_EXCEPTIONS
246 template <class _CharT
, class _Traits
>
247 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(short __n
) {
248 # if _LIBCPP_HAS_EXCEPTIONS
250 # endif // _LIBCPP_HAS_EXCEPTIONS
253 ios_base::fmtflags __flags
= ios_base::flags() & ios_base::basefield
;
254 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
255 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
259 __flags
== ios_base::oct
|| __flags
== ios_base::hex
260 ? static_cast<long>(static_cast<unsigned short>(__n
))
261 : static_cast<long>(__n
))
263 this->setstate(ios_base::badbit
| ios_base::failbit
);
265 # if _LIBCPP_HAS_EXCEPTIONS
267 this->__set_badbit_and_consider_rethrow();
269 # endif // _LIBCPP_HAS_EXCEPTIONS
273 template <class _CharT
, class _Traits
>
274 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(unsigned short __n
) {
275 # if _LIBCPP_HAS_EXCEPTIONS
277 # endif // _LIBCPP_HAS_EXCEPTIONS
280 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
281 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
282 if (__f
.put(*this, *this, this->fill(), static_cast<unsigned long>(__n
)).failed())
283 this->setstate(ios_base::badbit
| ios_base::failbit
);
285 # if _LIBCPP_HAS_EXCEPTIONS
287 this->__set_badbit_and_consider_rethrow();
289 # endif // _LIBCPP_HAS_EXCEPTIONS
293 template <class _CharT
, class _Traits
>
294 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(int __n
) {
295 # if _LIBCPP_HAS_EXCEPTIONS
297 # endif // _LIBCPP_HAS_EXCEPTIONS
300 ios_base::fmtflags __flags
= ios_base::flags() & ios_base::basefield
;
301 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
302 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
306 __flags
== ios_base::oct
|| __flags
== ios_base::hex
307 ? static_cast<long>(static_cast<unsigned int>(__n
))
308 : static_cast<long>(__n
))
310 this->setstate(ios_base::badbit
| ios_base::failbit
);
312 # if _LIBCPP_HAS_EXCEPTIONS
314 this->__set_badbit_and_consider_rethrow();
316 # endif // _LIBCPP_HAS_EXCEPTIONS
320 template <class _CharT
, class _Traits
>
321 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(unsigned int __n
) {
322 # if _LIBCPP_HAS_EXCEPTIONS
324 # endif // _LIBCPP_HAS_EXCEPTIONS
327 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
328 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
329 if (__f
.put(*this, *this, this->fill(), static_cast<unsigned long>(__n
)).failed())
330 this->setstate(ios_base::badbit
| ios_base::failbit
);
332 # if _LIBCPP_HAS_EXCEPTIONS
334 this->__set_badbit_and_consider_rethrow();
336 # endif // _LIBCPP_HAS_EXCEPTIONS
340 template <class _CharT
, class _Traits
>
341 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(long __n
) {
342 # if _LIBCPP_HAS_EXCEPTIONS
344 # endif // _LIBCPP_HAS_EXCEPTIONS
347 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
348 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
349 if (__f
.put(*this, *this, this->fill(), __n
).failed())
350 this->setstate(ios_base::badbit
| ios_base::failbit
);
352 # if _LIBCPP_HAS_EXCEPTIONS
354 this->__set_badbit_and_consider_rethrow();
356 # endif // _LIBCPP_HAS_EXCEPTIONS
360 template <class _CharT
, class _Traits
>
361 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(unsigned long __n
) {
362 # if _LIBCPP_HAS_EXCEPTIONS
364 # endif // _LIBCPP_HAS_EXCEPTIONS
367 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
368 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
369 if (__f
.put(*this, *this, this->fill(), __n
).failed())
370 this->setstate(ios_base::badbit
| ios_base::failbit
);
372 # if _LIBCPP_HAS_EXCEPTIONS
374 this->__set_badbit_and_consider_rethrow();
376 # endif // _LIBCPP_HAS_EXCEPTIONS
380 template <class _CharT
, class _Traits
>
381 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(long long __n
) {
382 # if _LIBCPP_HAS_EXCEPTIONS
384 # endif // _LIBCPP_HAS_EXCEPTIONS
387 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
388 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
389 if (__f
.put(*this, *this, this->fill(), __n
).failed())
390 this->setstate(ios_base::badbit
| ios_base::failbit
);
392 # if _LIBCPP_HAS_EXCEPTIONS
394 this->__set_badbit_and_consider_rethrow();
396 # endif // _LIBCPP_HAS_EXCEPTIONS
400 template <class _CharT
, class _Traits
>
401 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(unsigned long long __n
) {
402 # if _LIBCPP_HAS_EXCEPTIONS
404 # endif // _LIBCPP_HAS_EXCEPTIONS
407 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
408 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
409 if (__f
.put(*this, *this, this->fill(), __n
).failed())
410 this->setstate(ios_base::badbit
| ios_base::failbit
);
412 # if _LIBCPP_HAS_EXCEPTIONS
414 this->__set_badbit_and_consider_rethrow();
416 # endif // _LIBCPP_HAS_EXCEPTIONS
420 template <class _CharT
, class _Traits
>
421 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(float __n
) {
422 # if _LIBCPP_HAS_EXCEPTIONS
424 # endif // _LIBCPP_HAS_EXCEPTIONS
427 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
428 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
429 if (__f
.put(*this, *this, this->fill(), static_cast<double>(__n
)).failed())
430 this->setstate(ios_base::badbit
| ios_base::failbit
);
432 # if _LIBCPP_HAS_EXCEPTIONS
434 this->__set_badbit_and_consider_rethrow();
436 # endif // _LIBCPP_HAS_EXCEPTIONS
440 template <class _CharT
, class _Traits
>
441 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(double __n
) {
442 # if _LIBCPP_HAS_EXCEPTIONS
444 # endif // _LIBCPP_HAS_EXCEPTIONS
447 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
448 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
449 if (__f
.put(*this, *this, this->fill(), __n
).failed())
450 this->setstate(ios_base::badbit
| ios_base::failbit
);
452 # if _LIBCPP_HAS_EXCEPTIONS
454 this->__set_badbit_and_consider_rethrow();
456 # endif // _LIBCPP_HAS_EXCEPTIONS
460 template <class _CharT
, class _Traits
>
461 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(long double __n
) {
462 # if _LIBCPP_HAS_EXCEPTIONS
464 # endif // _LIBCPP_HAS_EXCEPTIONS
467 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
468 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
469 if (__f
.put(*this, *this, this->fill(), __n
).failed())
470 this->setstate(ios_base::badbit
| ios_base::failbit
);
472 # if _LIBCPP_HAS_EXCEPTIONS
474 this->__set_badbit_and_consider_rethrow();
476 # endif // _LIBCPP_HAS_EXCEPTIONS
480 template <class _CharT
, class _Traits
>
481 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(const void* __n
) {
482 # if _LIBCPP_HAS_EXCEPTIONS
484 # endif // _LIBCPP_HAS_EXCEPTIONS
487 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
488 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
489 if (__f
.put(*this, *this, this->fill(), __n
).failed())
490 this->setstate(ios_base::badbit
| ios_base::failbit
);
492 # if _LIBCPP_HAS_EXCEPTIONS
494 this->__set_badbit_and_consider_rethrow();
496 # endif // _LIBCPP_HAS_EXCEPTIONS
500 template <class _CharT
, class _Traits
>
501 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>& operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, _CharT __c
) {
502 return std::__put_character_sequence(__os
, &__c
, 1);
505 template <class _CharT
, class _Traits
>
506 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>& operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, char __cn
) {
507 # if _LIBCPP_HAS_EXCEPTIONS
509 # endif // _LIBCPP_HAS_EXCEPTIONS
510 typename basic_ostream
<_CharT
, _Traits
>::sentry
__s(__os
);
512 _CharT __c
= __os
.widen(__cn
);
513 typedef ostreambuf_iterator
<_CharT
, _Traits
> _Ip
;
514 if (std::__pad_and_output(
517 (__os
.flags() & ios_base::adjustfield
) == ios_base::left
? &__c
+ 1 : &__c
,
522 __os
.setstate(ios_base::badbit
| ios_base::failbit
);
524 # if _LIBCPP_HAS_EXCEPTIONS
526 __os
.__set_badbit_and_consider_rethrow();
528 # endif // _LIBCPP_HAS_EXCEPTIONS
532 template <class _Traits
>
533 _LIBCPP_HIDE_FROM_ABI basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>& __os
, char __c
) {
534 return std::__put_character_sequence(__os
, &__c
, 1);
537 template <class _Traits
>
538 _LIBCPP_HIDE_FROM_ABI basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>& __os
, signed char __c
) {
539 return std::__put_character_sequence(__os
, (char*)&__c
, 1);
542 template <class _Traits
>
543 _LIBCPP_HIDE_FROM_ABI basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>& __os
, unsigned char __c
) {
544 return std::__put_character_sequence(__os
, (char*)&__c
, 1);
547 template <class _CharT
, class _Traits
>
548 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
549 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const _CharT
* __str
) {
550 return std::__put_character_sequence(__os
, __str
, _Traits::length(__str
));
553 template <class _CharT
, class _Traits
>
554 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
555 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const char* __strn
) {
556 # if _LIBCPP_HAS_EXCEPTIONS
558 # endif // _LIBCPP_HAS_EXCEPTIONS
559 typename basic_ostream
<_CharT
, _Traits
>::sentry
__s(__os
);
561 typedef ostreambuf_iterator
<_CharT
, _Traits
> _Ip
;
562 size_t __len
= char_traits
<char>::length(__strn
);
563 const int __bs
= 100;
565 _CharT
* __wb
= __wbb
;
566 unique_ptr
<_CharT
, void (*)(void*)> __h(0, free
);
568 __wb
= (_CharT
*)malloc(__len
* sizeof(_CharT
));
573 for (_CharT
* __p
= __wb
; *__strn
!= '\0'; ++__strn
, ++__p
)
574 *__p
= __os
.widen(*__strn
);
575 if (std::__pad_and_output(
578 (__os
.flags() & ios_base::adjustfield
) == ios_base::left
? __wb
+ __len
: __wb
,
583 __os
.setstate(ios_base::badbit
| ios_base::failbit
);
585 # if _LIBCPP_HAS_EXCEPTIONS
587 __os
.__set_badbit_and_consider_rethrow();
589 # endif // _LIBCPP_HAS_EXCEPTIONS
593 template <class _Traits
>
594 _LIBCPP_HIDE_FROM_ABI basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>& __os
, const char* __str
) {
595 return std::__put_character_sequence(__os
, __str
, _Traits::length(__str
));
598 template <class _Traits
>
599 _LIBCPP_HIDE_FROM_ABI basic_ostream
<char, _Traits
>&
600 operator<<(basic_ostream
<char, _Traits
>& __os
, const signed char* __str
) {
601 const char* __s
= (const char*)__str
;
602 return std::__put_character_sequence(__os
, __s
, _Traits::length(__s
));
605 template <class _Traits
>
606 _LIBCPP_HIDE_FROM_ABI basic_ostream
<char, _Traits
>&
607 operator<<(basic_ostream
<char, _Traits
>& __os
, const unsigned char* __str
) {
608 const char* __s
= (const char*)__str
;
609 return std::__put_character_sequence(__os
, __s
, _Traits::length(__s
));
612 template <class _CharT
, class _Traits
>
613 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::put(char_type __c
) {
614 # if _LIBCPP_HAS_EXCEPTIONS
616 # endif // _LIBCPP_HAS_EXCEPTIONS
619 typedef ostreambuf_iterator
<_CharT
, _Traits
> _Op
;
623 this->setstate(ios_base::badbit
);
625 # if _LIBCPP_HAS_EXCEPTIONS
627 this->__set_badbit_and_consider_rethrow();
629 # endif // _LIBCPP_HAS_EXCEPTIONS
633 template <class _CharT
, class _Traits
>
634 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::write(const char_type
* __s
, streamsize __n
) {
635 # if _LIBCPP_HAS_EXCEPTIONS
637 # endif // _LIBCPP_HAS_EXCEPTIONS
640 if (this->rdbuf()->sputn(__s
, __n
) != __n
)
641 this->setstate(ios_base::badbit
);
643 # if _LIBCPP_HAS_EXCEPTIONS
645 this->__set_badbit_and_consider_rethrow();
647 # endif // _LIBCPP_HAS_EXCEPTIONS
651 template <class _CharT
, class _Traits
>
652 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::flush() {
653 # if _LIBCPP_HAS_EXCEPTIONS
655 # endif // _LIBCPP_HAS_EXCEPTIONS
659 if (this->rdbuf()->pubsync() == -1)
660 this->setstate(ios_base::badbit
);
663 # if _LIBCPP_HAS_EXCEPTIONS
665 this->__set_badbit_and_consider_rethrow();
667 # endif // _LIBCPP_HAS_EXCEPTIONS
671 template <class _CharT
, class _Traits
>
672 typename basic_ostream
<_CharT
, _Traits
>::pos_type basic_ostream
<_CharT
, _Traits
>::tellp() {
675 return this->rdbuf()->pubseekoff(0, ios_base::cur
, ios_base::out
);
678 template <class _CharT
, class _Traits
>
679 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::seekp(pos_type __pos
) {
682 if (this->rdbuf()->pubseekpos(__pos
, ios_base::out
) == pos_type(-1))
683 this->setstate(ios_base::failbit
);
688 template <class _CharT
, class _Traits
>
689 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::seekp(off_type __off
, ios_base::seekdir __dir
) {
692 if (this->rdbuf()->pubseekoff(__off
, __dir
, ios_base::out
) == pos_type(-1))
693 this->setstate(ios_base::failbit
);
698 template <class _CharT
, class _Traits
>
699 _LIBCPP_HIDE_FROM_ABI
inline basic_ostream
<_CharT
, _Traits
>& endl(basic_ostream
<_CharT
, _Traits
>& __os
) {
700 __os
.put(__os
.widen('\n'));
705 template <class _CharT
, class _Traits
>
706 _LIBCPP_HIDE_FROM_ABI
inline basic_ostream
<_CharT
, _Traits
>& ends(basic_ostream
<_CharT
, _Traits
>& __os
) {
711 template <class _CharT
, class _Traits
>
712 _LIBCPP_HIDE_FROM_ABI
inline basic_ostream
<_CharT
, _Traits
>& flush(basic_ostream
<_CharT
, _Traits
>& __os
) {
717 template <class _Stream
, class _Tp
, class = void>
718 struct __is_ostreamable
: false_type
{};
720 template <class _Stream
, class _Tp
>
721 struct __is_ostreamable
<_Stream
, _Tp
, decltype(std::declval
<_Stream
>() << std::declval
<_Tp
>(), void())> : true_type
{};
723 template <class _Stream
,
725 __enable_if_t
<_And
<is_base_of
<ios_base
, _Stream
>, __is_ostreamable
<_Stream
&, const _Tp
&> >::value
, int> = 0>
726 _LIBCPP_HIDE_FROM_ABI _Stream
&& operator<<(_Stream
&& __os
, const _Tp
& __x
) {
728 return std::move(__os
);
731 template <class _CharT
, class _Traits
, class _Allocator
>
732 basic_ostream
<_CharT
, _Traits
>&
733 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const basic_string
<_CharT
, _Traits
, _Allocator
>& __str
) {
734 return std::__put_character_sequence(__os
, __str
.data(), __str
.size());
737 template <class _CharT
, class _Traits
>
738 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
739 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, basic_string_view
<_CharT
, _Traits
> __sv
) {
740 return std::__put_character_sequence(__os
, __sv
.data(), __sv
.size());
743 template <class _CharT
, class _Traits
>
744 inline _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
745 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const error_code
& __ec
) {
746 return __os
<< __ec
.category().name() << ':' << __ec
.value();
749 template <class _CharT
, class _Traits
, class _Yp
>
750 inline _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
751 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, shared_ptr
<_Yp
> const& __p
) {
752 return __os
<< __p
.get();
760 __enable_if_t
<is_same
<void,
761 __void_t
<decltype((std::declval
<basic_ostream
<_CharT
, _Traits
>&>()
762 << std::declval
<typename unique_ptr
<_Yp
, _Dp
>::pointer
>()))> >::value
,
764 inline _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
765 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, unique_ptr
<_Yp
, _Dp
> const& __p
) {
766 return __os
<< __p
.get();
769 template <class _CharT
, class _Traits
, size_t _Size
>
770 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
771 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const bitset
<_Size
>& __x
) {
772 return __os
<< __x
.template to_string
<_CharT
, _Traits
>(std::use_facet
<ctype
<_CharT
> >(__os
.getloc()).widen('0'),
773 std::use_facet
<ctype
<_CharT
> >(__os
.getloc()).widen('1'));
776 # if _LIBCPP_STD_VER >= 20
778 # if _LIBCPP_HAS_WIDE_CHARACTERS
779 template <class _Traits
>
780 basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>&, wchar_t) = delete;
782 template <class _Traits
>
783 basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>&, const wchar_t*) = delete;
785 template <class _Traits
>
786 basic_ostream
<wchar_t, _Traits
>& operator<<(basic_ostream
<wchar_t, _Traits
>&, char16_t
) = delete;
788 template <class _Traits
>
789 basic_ostream
<wchar_t, _Traits
>& operator<<(basic_ostream
<wchar_t, _Traits
>&, char32_t
) = delete;
791 template <class _Traits
>
792 basic_ostream
<wchar_t, _Traits
>& operator<<(basic_ostream
<wchar_t, _Traits
>&, const char16_t
*) = delete;
794 template <class _Traits
>
795 basic_ostream
<wchar_t, _Traits
>& operator<<(basic_ostream
<wchar_t, _Traits
>&, const char32_t
*) = delete;
797 # endif // _LIBCPP_HAS_WIDE_CHARACTERS
799 # if _LIBCPP_HAS_CHAR8_T
800 template <class _Traits
>
801 basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>&, char8_t
) = delete;
803 template <class _Traits
>
804 basic_ostream
<wchar_t, _Traits
>& operator<<(basic_ostream
<wchar_t, _Traits
>&, char8_t
) = delete;
806 template <class _Traits
>
807 basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>&, const char8_t
*) = delete;
809 template <class _Traits
>
810 basic_ostream
<wchar_t, _Traits
>& operator<<(basic_ostream
<wchar_t, _Traits
>&, const char8_t
*) = delete;
813 template <class _Traits
>
814 basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>&, char16_t
) = delete;
816 template <class _Traits
>
817 basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>&, char32_t
) = delete;
819 template <class _Traits
>
820 basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>&, const char16_t
*) = delete;
822 template <class _Traits
>
823 basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>&, const char32_t
*) = delete;
825 # endif // _LIBCPP_STD_VER >= 20
827 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream
<char>;
828 # if _LIBCPP_HAS_WIDE_CHARACTERS
829 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream
<wchar_t>;
832 _LIBCPP_END_NAMESPACE_STD
836 #endif // _LIBCPP_HAS_LOCALIZATION
838 #endif // _LIBCPP___OSTREAM_BASIC_OSTREAM_H