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
12 #include <__cxx03/__config>
13 #include <__cxx03/__exception/operations.h>
14 #include <__cxx03/__memory/shared_ptr.h>
15 #include <__cxx03/__memory/unique_ptr.h>
16 #include <__cxx03/__system_error/error_code.h>
17 #include <__cxx03/__type_traits/conjunction.h>
18 #include <__cxx03/__type_traits/enable_if.h>
19 #include <__cxx03/__type_traits/is_base_of.h>
20 #include <__cxx03/__type_traits/void_t.h>
21 #include <__cxx03/__utility/declval.h>
22 #include <__cxx03/bitset>
23 #include <__cxx03/cstddef>
24 #include <__cxx03/ios>
25 #include <__cxx03/locale>
26 #include <__cxx03/new> // for __throw_bad_alloc
27 #include <__cxx03/streambuf>
28 #include <__cxx03/string_view>
30 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
31 # pragma GCC system_header
35 #include <__cxx03/__undef_macros>
37 _LIBCPP_BEGIN_NAMESPACE_STD
39 template <class _CharT
, class _Traits
>
40 class _LIBCPP_TEMPLATE_VIS basic_ostream
: virtual public basic_ios
<_CharT
, _Traits
> {
42 // types (inherited from basic_ios (27.5.4)):
43 typedef _CharT char_type
;
44 typedef _Traits traits_type
;
45 typedef typename
traits_type::int_type int_type
;
46 typedef typename
traits_type::pos_type pos_type
;
47 typedef typename
traits_type::off_type off_type
;
49 // 27.7.2.2 Constructor/destructor:
50 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
explicit basic_ostream(basic_streambuf
<char_type
, traits_type
>* __sb
) {
53 ~basic_ostream() override
;
55 basic_ostream(const basic_ostream
& __rhs
) = delete;
56 basic_ostream
& operator=(const basic_ostream
& __rhs
) = delete;
59 inline _LIBCPP_HIDE_FROM_ABI
basic_ostream(basic_ostream
&& __rhs
);
61 // 27.7.2.3 Assign/swap
62 inline _LIBCPP_HIDE_FROM_ABI basic_ostream
& operator=(basic_ostream
&& __rhs
);
64 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
void swap(basic_ostream
& __rhs
) {
65 basic_ios
<char_type
, traits_type
>::swap(__rhs
);
69 // 27.7.2.4 Prefix/suffix:
70 class _LIBCPP_TEMPLATE_VIS sentry
;
72 // 27.7.2.6 Formatted output:
73 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream
& operator<<(basic_ostream
& (*__pf
)(basic_ostream
&)) {
77 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream
&
78 operator<<(basic_ios
<char_type
, traits_type
>& (*__pf
)(basic_ios
<char_type
, traits_type
>&)) {
83 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream
& operator<<(ios_base
& (*__pf
)(ios_base
&)) {
88 basic_ostream
& operator<<(bool __n
);
89 basic_ostream
& operator<<(short __n
);
90 basic_ostream
& operator<<(unsigned short __n
);
91 basic_ostream
& operator<<(int __n
);
92 basic_ostream
& operator<<(unsigned int __n
);
93 basic_ostream
& operator<<(long __n
);
94 basic_ostream
& operator<<(unsigned long __n
);
95 basic_ostream
& operator<<(long long __n
);
96 basic_ostream
& operator<<(unsigned long long __n
);
97 basic_ostream
& operator<<(float __f
);
98 basic_ostream
& operator<<(double __f
);
99 basic_ostream
& operator<<(long double __f
);
100 basic_ostream
& operator<<(const void* __p
);
102 #if _LIBCPP_STD_VER >= 23
103 _LIBCPP_HIDE_FROM_ABI basic_ostream
& operator<<(const volatile void* __p
) {
104 return operator<<(const_cast<const void*>(__p
));
108 basic_ostream
& operator<<(basic_streambuf
<char_type
, traits_type
>* __sb
);
110 #if _LIBCPP_STD_VER >= 17
111 // LWG 2221 - nullptr. This is not backported to older standards modes.
112 // See https://reviews.llvm.org/D127033 for more info on the rationale.
113 _LIBCPP_HIDE_FROM_ABI basic_ostream
& operator<<(nullptr_t
) { return *this << "nullptr"; }
116 // 27.7.2.7 Unformatted output:
117 basic_ostream
& put(char_type __c
);
118 basic_ostream
& write(const char_type
* __s
, streamsize __n
);
119 basic_ostream
& flush();
122 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 pos_type
tellp();
123 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream
& seekp(pos_type __pos
);
124 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream
& seekp(off_type __off
, ios_base::seekdir __dir
);
127 _LIBCPP_HIDE_FROM_ABI
basic_ostream() {} // extension, intentially does not initialize
130 template <class _CharT
, class _Traits
>
131 class _LIBCPP_TEMPLATE_VIS basic_ostream
<_CharT
, _Traits
>::sentry
{
133 basic_ostream
<_CharT
, _Traits
>& __os_
;
136 explicit sentry(basic_ostream
<_CharT
, _Traits
>& __os
);
138 sentry(const sentry
&) = delete;
139 sentry
& operator=(const sentry
&) = delete;
141 _LIBCPP_HIDE_FROM_ABI
explicit operator bool() const { return __ok_
; }
144 template <class _CharT
, class _Traits
>
145 basic_ostream
<_CharT
, _Traits
>::sentry::sentry(basic_ostream
<_CharT
, _Traits
>& __os
) : __ok_(false), __os_(__os
) {
153 template <class _CharT
, class _Traits
>
154 basic_ostream
<_CharT
, _Traits
>::sentry::~sentry() {
155 if (__os_
.rdbuf() && __os_
.good() && (__os_
.flags() & ios_base::unitbuf
) && !uncaught_exception()) {
156 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
158 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
159 if (__os_
.rdbuf()->pubsync() == -1)
160 __os_
.setstate(ios_base::badbit
);
161 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
164 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
168 template <class _CharT
, class _Traits
>
169 basic_ostream
<_CharT
, _Traits
>::basic_ostream(basic_ostream
&& __rhs
) {
173 template <class _CharT
, class _Traits
>
174 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator=(basic_ostream
&& __rhs
) {
179 template <class _CharT
, class _Traits
>
180 basic_ostream
<_CharT
, _Traits
>::~basic_ostream() {}
182 template <class _CharT
, class _Traits
>
183 basic_ostream
<_CharT
, _Traits
>&
184 basic_ostream
<_CharT
, _Traits
>::operator<<(basic_streambuf
<char_type
, traits_type
>* __sb
) {
185 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
187 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
191 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
193 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
194 typedef istreambuf_iterator
<_CharT
, _Traits
> _Ip
;
195 typedef ostreambuf_iterator
<_CharT
, _Traits
> _Op
;
200 for (; __i
!= __eof
; ++__i
, ++__o
, ++__c
) {
206 this->setstate(ios_base::failbit
);
207 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
209 this->__set_failbit_and_consider_rethrow();
211 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
213 this->setstate(ios_base::badbit
);
215 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
217 this->__set_badbit_and_consider_rethrow();
219 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
223 template <class _CharT
, class _Traits
>
224 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(bool __n
) {
225 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
227 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
230 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
231 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
232 if (__f
.put(*this, *this, this->fill(), __n
).failed())
233 this->setstate(ios_base::badbit
| ios_base::failbit
);
235 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
237 this->__set_badbit_and_consider_rethrow();
239 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
243 template <class _CharT
, class _Traits
>
244 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(short __n
) {
245 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
247 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
250 ios_base::fmtflags __flags
= ios_base::flags() & ios_base::basefield
;
251 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
252 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
256 __flags
== ios_base::oct
|| __flags
== ios_base::hex
257 ? static_cast<long>(static_cast<unsigned short>(__n
))
258 : static_cast<long>(__n
))
260 this->setstate(ios_base::badbit
| ios_base::failbit
);
262 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
264 this->__set_badbit_and_consider_rethrow();
266 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
270 template <class _CharT
, class _Traits
>
271 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(unsigned short __n
) {
272 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
274 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
277 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
278 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
279 if (__f
.put(*this, *this, this->fill(), static_cast<unsigned long>(__n
)).failed())
280 this->setstate(ios_base::badbit
| ios_base::failbit
);
282 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
284 this->__set_badbit_and_consider_rethrow();
286 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
290 template <class _CharT
, class _Traits
>
291 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(int __n
) {
292 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
294 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
297 ios_base::fmtflags __flags
= ios_base::flags() & ios_base::basefield
;
298 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
299 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
303 __flags
== ios_base::oct
|| __flags
== ios_base::hex
304 ? static_cast<long>(static_cast<unsigned int>(__n
))
305 : static_cast<long>(__n
))
307 this->setstate(ios_base::badbit
| ios_base::failbit
);
309 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
311 this->__set_badbit_and_consider_rethrow();
313 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
317 template <class _CharT
, class _Traits
>
318 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(unsigned int __n
) {
319 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
321 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
324 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
325 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
326 if (__f
.put(*this, *this, this->fill(), static_cast<unsigned long>(__n
)).failed())
327 this->setstate(ios_base::badbit
| ios_base::failbit
);
329 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
331 this->__set_badbit_and_consider_rethrow();
333 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
337 template <class _CharT
, class _Traits
>
338 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(long __n
) {
339 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
341 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
344 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
345 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
346 if (__f
.put(*this, *this, this->fill(), __n
).failed())
347 this->setstate(ios_base::badbit
| ios_base::failbit
);
349 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
351 this->__set_badbit_and_consider_rethrow();
353 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
357 template <class _CharT
, class _Traits
>
358 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(unsigned long __n
) {
359 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
361 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
364 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
365 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
366 if (__f
.put(*this, *this, this->fill(), __n
).failed())
367 this->setstate(ios_base::badbit
| ios_base::failbit
);
369 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
371 this->__set_badbit_and_consider_rethrow();
373 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
377 template <class _CharT
, class _Traits
>
378 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(long long __n
) {
379 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
381 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
384 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
385 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
386 if (__f
.put(*this, *this, this->fill(), __n
).failed())
387 this->setstate(ios_base::badbit
| ios_base::failbit
);
389 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
391 this->__set_badbit_and_consider_rethrow();
393 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
397 template <class _CharT
, class _Traits
>
398 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(unsigned long long __n
) {
399 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
401 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
404 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
405 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
406 if (__f
.put(*this, *this, this->fill(), __n
).failed())
407 this->setstate(ios_base::badbit
| ios_base::failbit
);
409 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
411 this->__set_badbit_and_consider_rethrow();
413 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
417 template <class _CharT
, class _Traits
>
418 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(float __n
) {
419 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
421 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
424 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
425 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
426 if (__f
.put(*this, *this, this->fill(), static_cast<double>(__n
)).failed())
427 this->setstate(ios_base::badbit
| ios_base::failbit
);
429 #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
>& basic_ostream
<_CharT
, _Traits
>::operator<<(double __n
) {
439 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
441 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
444 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
445 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
446 if (__f
.put(*this, *this, this->fill(), __n
).failed())
447 this->setstate(ios_base::badbit
| ios_base::failbit
);
449 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
451 this->__set_badbit_and_consider_rethrow();
453 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
457 template <class _CharT
, class _Traits
>
458 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(long double __n
) {
459 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
461 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
464 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
465 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
466 if (__f
.put(*this, *this, this->fill(), __n
).failed())
467 this->setstate(ios_base::badbit
| ios_base::failbit
);
469 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
471 this->__set_badbit_and_consider_rethrow();
473 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
477 template <class _CharT
, class _Traits
>
478 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::operator<<(const void* __n
) {
479 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
481 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
484 typedef num_put
<char_type
, ostreambuf_iterator
<char_type
, traits_type
> > _Fp
;
485 const _Fp
& __f
= std::use_facet
<_Fp
>(this->getloc());
486 if (__f
.put(*this, *this, this->fill(), __n
).failed())
487 this->setstate(ios_base::badbit
| ios_base::failbit
);
489 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
491 this->__set_badbit_and_consider_rethrow();
493 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
497 template <class _CharT
, class _Traits
>
498 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
499 __put_character_sequence(basic_ostream
<_CharT
, _Traits
>& __os
, const _CharT
* __str
, size_t __len
) {
500 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
502 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
503 typename basic_ostream
<_CharT
, _Traits
>::sentry
__s(__os
);
505 typedef ostreambuf_iterator
<_CharT
, _Traits
> _Ip
;
506 if (std::__pad_and_output(
509 (__os
.flags() & ios_base::adjustfield
) == ios_base::left
? __str
+ __len
: __str
,
514 __os
.setstate(ios_base::badbit
| ios_base::failbit
);
516 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
518 __os
.__set_badbit_and_consider_rethrow();
520 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
524 template <class _CharT
, class _Traits
>
525 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>& operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, _CharT __c
) {
526 return std::__put_character_sequence(__os
, &__c
, 1);
529 template <class _CharT
, class _Traits
>
530 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>& operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, char __cn
) {
531 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
533 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
534 typename basic_ostream
<_CharT
, _Traits
>::sentry
__s(__os
);
536 _CharT __c
= __os
.widen(__cn
);
537 typedef ostreambuf_iterator
<_CharT
, _Traits
> _Ip
;
538 if (std::__pad_and_output(
541 (__os
.flags() & ios_base::adjustfield
) == ios_base::left
? &__c
+ 1 : &__c
,
546 __os
.setstate(ios_base::badbit
| ios_base::failbit
);
548 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
550 __os
.__set_badbit_and_consider_rethrow();
552 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
556 template <class _Traits
>
557 _LIBCPP_HIDE_FROM_ABI basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>& __os
, char __c
) {
558 return std::__put_character_sequence(__os
, &__c
, 1);
561 template <class _Traits
>
562 _LIBCPP_HIDE_FROM_ABI basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>& __os
, signed char __c
) {
563 return std::__put_character_sequence(__os
, (char*)&__c
, 1);
566 template <class _Traits
>
567 _LIBCPP_HIDE_FROM_ABI basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>& __os
, unsigned char __c
) {
568 return std::__put_character_sequence(__os
, (char*)&__c
, 1);
571 template <class _CharT
, class _Traits
>
572 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
573 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const _CharT
* __str
) {
574 return std::__put_character_sequence(__os
, __str
, _Traits::length(__str
));
577 template <class _CharT
, class _Traits
>
578 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
579 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const char* __strn
) {
580 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
582 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
583 typename basic_ostream
<_CharT
, _Traits
>::sentry
__s(__os
);
585 typedef ostreambuf_iterator
<_CharT
, _Traits
> _Ip
;
586 size_t __len
= char_traits
<char>::length(__strn
);
587 const int __bs
= 100;
589 _CharT
* __wb
= __wbb
;
590 unique_ptr
<_CharT
, void (*)(void*)> __h(0, free
);
592 __wb
= (_CharT
*)malloc(__len
* sizeof(_CharT
));
597 for (_CharT
* __p
= __wb
; *__strn
!= '\0'; ++__strn
, ++__p
)
598 *__p
= __os
.widen(*__strn
);
599 if (std::__pad_and_output(
602 (__os
.flags() & ios_base::adjustfield
) == ios_base::left
? __wb
+ __len
: __wb
,
607 __os
.setstate(ios_base::badbit
| ios_base::failbit
);
609 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
611 __os
.__set_badbit_and_consider_rethrow();
613 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
617 template <class _Traits
>
618 _LIBCPP_HIDE_FROM_ABI basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>& __os
, const char* __str
) {
619 return std::__put_character_sequence(__os
, __str
, _Traits::length(__str
));
622 template <class _Traits
>
623 _LIBCPP_HIDE_FROM_ABI basic_ostream
<char, _Traits
>&
624 operator<<(basic_ostream
<char, _Traits
>& __os
, const signed char* __str
) {
625 const char* __s
= (const char*)__str
;
626 return std::__put_character_sequence(__os
, __s
, _Traits::length(__s
));
629 template <class _Traits
>
630 _LIBCPP_HIDE_FROM_ABI basic_ostream
<char, _Traits
>&
631 operator<<(basic_ostream
<char, _Traits
>& __os
, const unsigned char* __str
) {
632 const char* __s
= (const char*)__str
;
633 return std::__put_character_sequence(__os
, __s
, _Traits::length(__s
));
636 template <class _CharT
, class _Traits
>
637 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::put(char_type __c
) {
638 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
640 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
643 typedef ostreambuf_iterator
<_CharT
, _Traits
> _Op
;
647 this->setstate(ios_base::badbit
);
649 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
651 this->__set_badbit_and_consider_rethrow();
653 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
657 template <class _CharT
, class _Traits
>
658 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::write(const char_type
* __s
, streamsize __n
) {
659 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
661 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
664 if (this->rdbuf()->sputn(__s
, __n
) != __n
)
665 this->setstate(ios_base::badbit
);
667 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
669 this->__set_badbit_and_consider_rethrow();
671 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
675 template <class _CharT
, class _Traits
>
676 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::flush() {
677 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
679 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
683 if (this->rdbuf()->pubsync() == -1)
684 this->setstate(ios_base::badbit
);
687 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
689 this->__set_badbit_and_consider_rethrow();
691 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
695 template <class _CharT
, class _Traits
>
696 typename basic_ostream
<_CharT
, _Traits
>::pos_type basic_ostream
<_CharT
, _Traits
>::tellp() {
699 return this->rdbuf()->pubseekoff(0, ios_base::cur
, ios_base::out
);
702 template <class _CharT
, class _Traits
>
703 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::seekp(pos_type __pos
) {
706 if (this->rdbuf()->pubseekpos(__pos
, ios_base::out
) == pos_type(-1))
707 this->setstate(ios_base::failbit
);
712 template <class _CharT
, class _Traits
>
713 basic_ostream
<_CharT
, _Traits
>& basic_ostream
<_CharT
, _Traits
>::seekp(off_type __off
, ios_base::seekdir __dir
) {
716 if (this->rdbuf()->pubseekoff(__off
, __dir
, ios_base::out
) == pos_type(-1))
717 this->setstate(ios_base::failbit
);
722 template <class _CharT
, class _Traits
>
723 _LIBCPP_HIDE_FROM_ABI
inline basic_ostream
<_CharT
, _Traits
>& endl(basic_ostream
<_CharT
, _Traits
>& __os
) {
724 __os
.put(__os
.widen('\n'));
729 template <class _CharT
, class _Traits
>
730 _LIBCPP_HIDE_FROM_ABI
inline basic_ostream
<_CharT
, _Traits
>& ends(basic_ostream
<_CharT
, _Traits
>& __os
) {
735 template <class _CharT
, class _Traits
>
736 _LIBCPP_HIDE_FROM_ABI
inline basic_ostream
<_CharT
, _Traits
>& flush(basic_ostream
<_CharT
, _Traits
>& __os
) {
741 template <class _Stream
, class _Tp
, class = void>
742 struct __is_ostreamable
: false_type
{};
744 template <class _Stream
, class _Tp
>
745 struct __is_ostreamable
<_Stream
, _Tp
, decltype(std::declval
<_Stream
>() << std::declval
<_Tp
>(), void())> : true_type
{};
747 template <class _Stream
,
749 __enable_if_t
<_And
<is_base_of
<ios_base
, _Stream
>, __is_ostreamable
<_Stream
&, const _Tp
&> >::value
, int> = 0>
750 _LIBCPP_HIDE_FROM_ABI _Stream
&& operator<<(_Stream
&& __os
, const _Tp
& __x
) {
752 return std::move(__os
);
755 template <class _CharT
, class _Traits
, class _Allocator
>
756 basic_ostream
<_CharT
, _Traits
>&
757 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const basic_string
<_CharT
, _Traits
, _Allocator
>& __str
) {
758 return std::__put_character_sequence(__os
, __str
.data(), __str
.size());
761 template <class _CharT
, class _Traits
>
762 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
763 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, basic_string_view
<_CharT
, _Traits
> __sv
) {
764 return std::__put_character_sequence(__os
, __sv
.data(), __sv
.size());
767 template <class _CharT
, class _Traits
>
768 inline _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
769 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const error_code
& __ec
) {
770 return __os
<< __ec
.category().name() << ':' << __ec
.value();
773 template <class _CharT
, class _Traits
, class _Yp
>
774 inline _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
775 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, shared_ptr
<_Yp
> const& __p
) {
776 return __os
<< __p
.get();
784 __enable_if_t
<is_same
<void,
785 __void_t
<decltype((std::declval
<basic_ostream
<_CharT
, _Traits
>&>()
786 << std::declval
<typename unique_ptr
<_Yp
, _Dp
>::pointer
>()))> >::value
,
788 inline _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
789 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, unique_ptr
<_Yp
, _Dp
> const& __p
) {
790 return __os
<< __p
.get();
793 template <class _CharT
, class _Traits
, size_t _Size
>
794 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
795 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const bitset
<_Size
>& __x
) {
796 return __os
<< __x
.template to_string
<_CharT
, _Traits
>(std::use_facet
<ctype
<_CharT
> >(__os
.getloc()).widen('0'),
797 std::use_facet
<ctype
<_CharT
> >(__os
.getloc()).widen('1'));
800 #if _LIBCPP_STD_VER >= 20
802 # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
803 template <class _Traits
>
804 basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>&, wchar_t) = delete;
806 template <class _Traits
>
807 basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>&, const wchar_t*) = delete;
809 template <class _Traits
>
810 basic_ostream
<wchar_t, _Traits
>& operator<<(basic_ostream
<wchar_t, _Traits
>&, char16_t
) = delete;
812 template <class _Traits
>
813 basic_ostream
<wchar_t, _Traits
>& operator<<(basic_ostream
<wchar_t, _Traits
>&, char32_t
) = delete;
815 template <class _Traits
>
816 basic_ostream
<wchar_t, _Traits
>& operator<<(basic_ostream
<wchar_t, _Traits
>&, const char16_t
*) = delete;
818 template <class _Traits
>
819 basic_ostream
<wchar_t, _Traits
>& operator<<(basic_ostream
<wchar_t, _Traits
>&, const char32_t
*) = delete;
821 # endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
823 # ifndef _LIBCPP_HAS_NO_CHAR8_T
824 template <class _Traits
>
825 basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>&, char8_t
) = delete;
827 template <class _Traits
>
828 basic_ostream
<wchar_t, _Traits
>& operator<<(basic_ostream
<wchar_t, _Traits
>&, char8_t
) = delete;
830 template <class _Traits
>
831 basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>&, const char8_t
*) = delete;
833 template <class _Traits
>
834 basic_ostream
<wchar_t, _Traits
>& operator<<(basic_ostream
<wchar_t, _Traits
>&, const char8_t
*) = delete;
837 template <class _Traits
>
838 basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>&, char16_t
) = delete;
840 template <class _Traits
>
841 basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>&, char32_t
) = delete;
843 template <class _Traits
>
844 basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>&, const char16_t
*) = delete;
846 template <class _Traits
>
847 basic_ostream
<char, _Traits
>& operator<<(basic_ostream
<char, _Traits
>&, const char32_t
*) = delete;
849 #endif // _LIBCPP_STD_VER >= 20
851 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream
<char>;
852 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
853 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream
<wchar_t>;
856 _LIBCPP_END_NAMESPACE_STD
860 #endif // _LIBCPP___OSTREAM_BASIC_OSTREAM_H