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_ISTREAM
11 #define _LIBCPP_ISTREAM
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.1.1.1 Constructor/destructor:
29 explicit basic_istream(basic_streambuf<char_type, traits_type>* sb);
30 basic_istream(basic_istream&& rhs);
31 virtual ~basic_istream();
33 // 27.7.1.1.2 Assign/swap:
34 basic_istream& operator=(basic_istream&& rhs);
35 void swap(basic_istream& rhs);
37 // 27.7.1.1.3 Prefix/suffix:
40 // 27.7.1.2 Formatted input:
41 basic_istream& operator>>(basic_istream& (*pf)(basic_istream&));
42 basic_istream& operator>>(basic_ios<char_type, traits_type>&
43 (*pf)(basic_ios<char_type, traits_type>&));
44 basic_istream& operator>>(ios_base& (*pf)(ios_base&));
45 basic_istream& operator>>(basic_streambuf<char_type, traits_type>* sb);
46 basic_istream& operator>>(bool& n);
47 basic_istream& operator>>(short& n);
48 basic_istream& operator>>(unsigned short& n);
49 basic_istream& operator>>(int& n);
50 basic_istream& operator>>(unsigned int& n);
51 basic_istream& operator>>(long& n);
52 basic_istream& operator>>(unsigned long& n);
53 basic_istream& operator>>(long long& n);
54 basic_istream& operator>>(unsigned long long& n);
55 basic_istream& operator>>(float& f);
56 basic_istream& operator>>(double& f);
57 basic_istream& operator>>(long double& f);
58 basic_istream& operator>>(void*& p);
60 // 27.7.1.3 Unformatted input:
61 streamsize gcount() const;
63 basic_istream& get(char_type& c);
64 basic_istream& get(char_type* s, streamsize n);
65 basic_istream& get(char_type* s, streamsize n, char_type delim);
66 basic_istream& get(basic_streambuf<char_type,traits_type>& sb);
67 basic_istream& get(basic_streambuf<char_type,traits_type>& sb, char_type delim);
69 basic_istream& getline(char_type* s, streamsize n);
70 basic_istream& getline(char_type* s, streamsize n, char_type delim);
72 basic_istream& ignore(streamsize n = 1, int_type delim = traits_type::eof());
74 basic_istream& read (char_type* s, streamsize n);
75 streamsize readsome(char_type* s, streamsize n);
77 basic_istream& putback(char_type c);
78 basic_istream& unget();
82 basic_istream& seekg(pos_type);
83 basic_istream& seekg(off_type, ios_base::seekdir);
85 basic_istream(const basic_istream& rhs) = delete;
86 basic_istream(basic_istream&& rhs);
87 // 27.7.2.1.2 Assign/swap:
88 basic_istream& operator=(const basic_istream& rhs) = delete;
89 basic_istream& operator=(basic_istream&& rhs);
90 void swap(basic_istream& rhs);
93 // 27.7.1.2.3 character extraction templates:
94 template<class charT, class traits>
95 basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&, charT&);
97 template<class traits>
98 basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, unsigned char&);
100 template<class traits>
101 basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, signed char&);
103 template<class charT, class traits>
104 basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&, charT*);
106 template<class traits>
107 basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, unsigned char*);
109 template<class traits>
110 basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, signed char*);
112 template <class charT, class traits>
114 swap(basic_istream<charT, traits>& x, basic_istream<charT, traits>& y);
116 typedef basic_istream<char> istream;
117 typedef basic_istream<wchar_t> wistream;
119 template <class charT, class traits = char_traits<charT> >
120 class basic_iostream :
121 public basic_istream<charT,traits>,
122 public basic_ostream<charT,traits>
126 typedef charT char_type;
127 typedef traits traits_type;
128 typedef typename traits_type::int_type int_type;
129 typedef typename traits_type::pos_type pos_type;
130 typedef typename traits_type::off_type off_type;
132 // constructor/destructor
133 explicit basic_iostream(basic_streambuf<char_type, traits_type>* sb);
134 basic_iostream(basic_iostream&& rhs);
135 virtual ~basic_iostream();
138 basic_iostream& operator=(basic_iostream&& rhs);
139 void swap(basic_iostream& rhs);
142 template <class charT, class traits>
144 swap(basic_iostream<charT, traits>& x, basic_iostream<charT, traits>& y);
146 typedef basic_iostream<char> iostream;
147 typedef basic_iostream<wchar_t> wiostream;
149 template <class charT, class traits>
150 basic_istream<charT,traits>&
151 ws(basic_istream<charT,traits>& is);
153 // rvalue stream extraction
154 template <class Stream, class T>
155 Stream&& operator>>(Stream&& is, T&& x);
161 #if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
162 # include <__cxx03/istream>
166 # if _LIBCPP_HAS_LOCALIZATION
168 # include <__fwd/istream.h>
169 # include <__iterator/istreambuf_iterator.h>
170 # include <__ostream/basic_ostream.h>
171 # include <__type_traits/conjunction.h>
172 # include <__type_traits/enable_if.h>
173 # include <__type_traits/is_base_of.h>
174 # include <__type_traits/make_unsigned.h>
175 # include <__utility/declval.h>
176 # include <__utility/forward.h>
182 # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
183 # pragma GCC system_header
187 # include <__undef_macros>
189 _LIBCPP_BEGIN_NAMESPACE_STD
191 template <class _CharT, class _Traits>
192 class _LIBCPP_TEMPLATE_VIS basic_istream : virtual public basic_ios<_CharT, _Traits> {
195 _LIBCPP_HIDE_FROM_ABI void __inc_gcount() {
196 if (__gc_ < numeric_limits<streamsize>::max())
201 // types (inherited from basic_ios (27.5.4)):
202 typedef _CharT char_type;
203 typedef _Traits traits_type;
204 typedef typename traits_type::int_type int_type;
205 typedef typename traits_type::pos_type pos_type;
206 typedef typename traits_type::off_type off_type;
208 // 27.7.1.1.1 Constructor/destructor:
209 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 explicit basic_istream(basic_streambuf<char_type, traits_type>* __sb)
213 ~basic_istream() override;
216 inline _LIBCPP_HIDE_FROM_ABI basic_istream(basic_istream&& __rhs);
218 // 27.7.1.1.2 Assign/swap:
219 inline _LIBCPP_HIDE_FROM_ABI basic_istream& operator=(basic_istream&& __rhs);
221 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void swap(basic_istream& __rhs) {
222 std::swap(__gc_, __rhs.__gc_);
223 basic_ios<char_type, traits_type>::swap(__rhs);
227 basic_istream(const basic_istream& __rhs) = delete;
228 basic_istream& operator=(const basic_istream& __rhs) = delete;
230 // 27.7.1.1.3 Prefix/suffix:
231 class _LIBCPP_TEMPLATE_VIS sentry;
233 // 27.7.1.2 Formatted input:
234 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& operator>>(basic_istream& (*__pf)(basic_istream&)) {
238 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream&
239 operator>>(basic_ios<char_type, traits_type>& (*__pf)(basic_ios<char_type, traits_type>&)) {
244 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& operator>>(ios_base& (*__pf)(ios_base&)) {
249 basic_istream& operator>>(basic_streambuf<char_type, traits_type>* __sb);
250 basic_istream& operator>>(bool& __n);
251 basic_istream& operator>>(short& __n);
252 basic_istream& operator>>(unsigned short& __n);
253 basic_istream& operator>>(int& __n);
254 basic_istream& operator>>(unsigned int& __n);
255 basic_istream& operator>>(long& __n);
256 basic_istream& operator>>(unsigned long& __n);
257 basic_istream& operator>>(long long& __n);
258 basic_istream& operator>>(unsigned long long& __n);
259 basic_istream& operator>>(float& __f);
260 basic_istream& operator>>(double& __f);
261 basic_istream& operator>>(long double& __f);
262 basic_istream& operator>>(void*& __p);
264 // 27.7.1.3 Unformatted input:
265 _LIBCPP_HIDE_FROM_ABI streamsize gcount() const { return __gc_; }
268 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& get(char_type& __c) {
269 int_type __ch = get();
270 if (__ch != traits_type::eof())
271 __c = traits_type::to_char_type(__ch);
275 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& get(char_type* __s, streamsize __n) {
276 return get(__s, __n, this->widen('\n'));
279 basic_istream& get(char_type* __s, streamsize __n, char_type __dlm);
281 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& get(basic_streambuf<char_type, traits_type>& __sb) {
282 return get(__sb, this->widen('\n'));
285 basic_istream& get(basic_streambuf<char_type, traits_type>& __sb, char_type __dlm);
287 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& getline(char_type* __s, streamsize __n) {
288 return getline(__s, __n, this->widen('\n'));
291 basic_istream& getline(char_type* __s, streamsize __n, char_type __dlm);
293 basic_istream& ignore(streamsize __n = 1, int_type __dlm = traits_type::eof());
295 basic_istream& read(char_type* __s, streamsize __n);
296 streamsize readsome(char_type* __s, streamsize __n);
298 basic_istream& putback(char_type __c);
299 basic_istream& unget();
303 basic_istream& seekg(pos_type __pos);
304 basic_istream& seekg(off_type __off, ios_base::seekdir __dir);
307 template <class _CharT, class _Traits>
308 class _LIBCPP_TEMPLATE_VIS basic_istream<_CharT, _Traits>::sentry {
312 explicit sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false);
313 // ~sentry() = default;
315 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const { return __ok_; }
317 sentry(const sentry&) = delete;
318 sentry& operator=(const sentry&) = delete;
321 template <class _CharT, class _Traits>
322 basic_istream<_CharT, _Traits>::sentry::sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws) : __ok_(false) {
326 if (!__noskipws && (__is.flags() & ios_base::skipws)) {
327 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
328 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
331 for (; __i != __eof; ++__i)
332 if (!__ct.is(__ct.space, *__i))
335 __is.setstate(ios_base::failbit | ios_base::eofbit);
339 __is.setstate(ios_base::failbit);
342 template <class _CharT, class _Traits>
343 basic_istream<_CharT, _Traits>::basic_istream(basic_istream&& __rhs) : __gc_(__rhs.__gc_) {
348 template <class _CharT, class _Traits>
349 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator=(basic_istream&& __rhs) {
354 template <class _CharT, class _Traits>
355 basic_istream<_CharT, _Traits>::~basic_istream() {}
357 template <class _Tp, class _CharT, class _Traits>
358 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
359 __input_arithmetic(basic_istream<_CharT, _Traits>& __is, _Tp& __n) {
360 ios_base::iostate __state = ios_base::goodbit;
361 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
363 # if _LIBCPP_HAS_EXCEPTIONS
365 # endif // _LIBCPP_HAS_EXCEPTIONS
366 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
367 typedef num_get<_CharT, _Ip> _Fp;
368 std::use_facet<_Fp>(__is.getloc()).get(_Ip(__is), _Ip(), __is, __state, __n);
369 # if _LIBCPP_HAS_EXCEPTIONS
371 __state |= ios_base::badbit;
372 __is.__setstate_nothrow(__state);
373 if (__is.exceptions() & ios_base::badbit) {
378 __is.setstate(__state);
383 template <class _CharT, class _Traits>
384 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(unsigned short& __n) {
385 return std::__input_arithmetic<unsigned short>(*this, __n);
388 template <class _CharT, class _Traits>
389 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(unsigned int& __n) {
390 return std::__input_arithmetic<unsigned int>(*this, __n);
393 template <class _CharT, class _Traits>
394 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(long& __n) {
395 return std::__input_arithmetic<long>(*this, __n);
398 template <class _CharT, class _Traits>
399 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(unsigned long& __n) {
400 return std::__input_arithmetic<unsigned long>(*this, __n);
403 template <class _CharT, class _Traits>
404 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(long long& __n) {
405 return std::__input_arithmetic<long long>(*this, __n);
408 template <class _CharT, class _Traits>
409 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(unsigned long long& __n) {
410 return std::__input_arithmetic<unsigned long long>(*this, __n);
413 template <class _CharT, class _Traits>
414 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(float& __n) {
415 return std::__input_arithmetic<float>(*this, __n);
418 template <class _CharT, class _Traits>
419 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(double& __n) {
420 return std::__input_arithmetic<double>(*this, __n);
423 template <class _CharT, class _Traits>
424 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(long double& __n) {
425 return std::__input_arithmetic<long double>(*this, __n);
428 template <class _CharT, class _Traits>
429 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(bool& __n) {
430 return std::__input_arithmetic<bool>(*this, __n);
433 template <class _CharT, class _Traits>
434 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(void*& __n) {
435 return std::__input_arithmetic<void*>(*this, __n);
438 template <class _Tp, class _CharT, class _Traits>
439 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
440 __input_arithmetic_with_numeric_limits(basic_istream<_CharT, _Traits>& __is, _Tp& __n) {
441 ios_base::iostate __state = ios_base::goodbit;
442 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
444 # if _LIBCPP_HAS_EXCEPTIONS
446 # endif // _LIBCPP_HAS_EXCEPTIONS
447 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
448 typedef num_get<_CharT, _Ip> _Fp;
450 std::use_facet<_Fp>(__is.getloc()).get(_Ip(__is), _Ip(), __is, __state, __temp);
451 if (__temp < numeric_limits<_Tp>::min()) {
452 __state |= ios_base::failbit;
453 __n = numeric_limits<_Tp>::min();
454 } else if (__temp > numeric_limits<_Tp>::max()) {
455 __state |= ios_base::failbit;
456 __n = numeric_limits<_Tp>::max();
458 __n = static_cast<_Tp>(__temp);
460 # if _LIBCPP_HAS_EXCEPTIONS
462 __state |= ios_base::badbit;
463 __is.__setstate_nothrow(__state);
464 if (__is.exceptions() & ios_base::badbit) {
468 # endif // _LIBCPP_HAS_EXCEPTIONS
469 __is.setstate(__state);
474 template <class _CharT, class _Traits>
475 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(short& __n) {
476 return std::__input_arithmetic_with_numeric_limits<short>(*this, __n);
479 template <class _CharT, class _Traits>
480 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(int& __n) {
481 return std::__input_arithmetic_with_numeric_limits<int>(*this, __n);
484 template <class _CharT, class _Traits>
485 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
486 __input_c_string(basic_istream<_CharT, _Traits>& __is, _CharT* __p, size_t __n) {
487 ios_base::iostate __state = ios_base::goodbit;
488 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
490 # if _LIBCPP_HAS_EXCEPTIONS
494 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
495 while (__s != __p + (__n - 1)) {
496 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
497 if (_Traits::eq_int_type(__i, _Traits::eof())) {
498 __state |= ios_base::eofbit;
501 _CharT __ch = _Traits::to_char_type(__i);
502 if (__ct.is(__ct.space, __ch))
505 __is.rdbuf()->sbumpc();
510 __state |= ios_base::failbit;
511 # if _LIBCPP_HAS_EXCEPTIONS
513 __state |= ios_base::badbit;
514 __is.__setstate_nothrow(__state);
515 if (__is.exceptions() & ios_base::badbit) {
520 __is.setstate(__state);
525 # if _LIBCPP_STD_VER >= 20
527 template <class _CharT, class _Traits, size_t _Np>
528 inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
529 operator>>(basic_istream<_CharT, _Traits>& __is, _CharT (&__buf)[_Np]) {
531 if (__is.width() > 0)
532 __n = std::min(size_t(__is.width()), _Np);
533 return std::__input_c_string(__is, __buf, __n);
536 template <class _Traits, size_t _Np>
537 inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
538 operator>>(basic_istream<char, _Traits>& __is, unsigned char (&__buf)[_Np]) {
539 return __is >> (char(&)[_Np])__buf;
542 template <class _Traits, size_t _Np>
543 inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
544 operator>>(basic_istream<char, _Traits>& __is, signed char (&__buf)[_Np]) {
545 return __is >> (char(&)[_Np])__buf;
550 template <class _CharT, class _Traits>
551 inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
552 operator>>(basic_istream<_CharT, _Traits>& __is, _CharT* __s) {
553 streamsize __n = __is.width();
555 __n = numeric_limits<streamsize>::max() / sizeof(_CharT) - 1;
556 return std::__input_c_string(__is, __s, size_t(__n));
559 template <class _Traits>
560 inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
561 operator>>(basic_istream<char, _Traits>& __is, unsigned char* __s) {
562 return __is >> (char*)__s;
565 template <class _Traits>
566 inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
567 operator>>(basic_istream<char, _Traits>& __is, signed char* __s) {
568 return __is >> (char*)__s;
571 # endif // _LIBCPP_STD_VER >= 20
573 template <class _CharT, class _Traits>
574 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& operator>>(basic_istream<_CharT, _Traits>& __is, _CharT& __c) {
575 ios_base::iostate __state = ios_base::goodbit;
576 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
578 # if _LIBCPP_HAS_EXCEPTIONS
581 typename _Traits::int_type __i = __is.rdbuf()->sbumpc();
582 if (_Traits::eq_int_type(__i, _Traits::eof()))
583 __state |= ios_base::eofbit | ios_base::failbit;
585 __c = _Traits::to_char_type(__i);
586 # if _LIBCPP_HAS_EXCEPTIONS
588 __state |= ios_base::badbit;
589 __is.__setstate_nothrow(__state);
590 if (__is.exceptions() & ios_base::badbit) {
595 __is.setstate(__state);
600 template <class _Traits>
601 inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
602 operator>>(basic_istream<char, _Traits>& __is, unsigned char& __c) {
603 return __is >> (char&)__c;
606 template <class _Traits>
607 inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
608 operator>>(basic_istream<char, _Traits>& __is, signed char& __c) {
609 return __is >> (char&)__c;
612 template <class _CharT, class _Traits>
613 basic_istream<_CharT, _Traits>&
614 basic_istream<_CharT, _Traits>::operator>>(basic_streambuf<char_type, traits_type>* __sb) {
615 ios_base::iostate __state = ios_base::goodbit;
617 sentry __s(*this, true);
620 # if _LIBCPP_HAS_EXCEPTIONS
622 # endif // _LIBCPP_HAS_EXCEPTIONS
624 typename traits_type::int_type __i = this->rdbuf()->sgetc();
625 if (traits_type::eq_int_type(__i, _Traits::eof())) {
626 __state |= ios_base::eofbit;
629 if (traits_type::eq_int_type(__sb->sputc(traits_type::to_char_type(__i)), traits_type::eof()))
632 this->rdbuf()->sbumpc();
635 __state |= ios_base::failbit;
636 # if _LIBCPP_HAS_EXCEPTIONS
638 __state |= ios_base::badbit;
640 __state |= ios_base::failbit;
642 this->__setstate_nothrow(__state);
643 if (this->exceptions() & ios_base::failbit || this->exceptions() & ios_base::badbit) {
647 # endif // _LIBCPP_HAS_EXCEPTIONS
649 __state |= ios_base::failbit;
651 this->setstate(__state);
656 template <class _CharT, class _Traits>
657 typename basic_istream<_CharT, _Traits>::int_type basic_istream<_CharT, _Traits>::get() {
658 ios_base::iostate __state = ios_base::goodbit;
660 int_type __r = traits_type::eof();
661 sentry __s(*this, true);
663 # if _LIBCPP_HAS_EXCEPTIONS
666 __r = this->rdbuf()->sbumpc();
667 if (traits_type::eq_int_type(__r, traits_type::eof()))
668 __state |= ios_base::failbit | ios_base::eofbit;
671 # if _LIBCPP_HAS_EXCEPTIONS
673 this->__setstate_nothrow(this->rdstate() | ios_base::badbit);
674 if (this->exceptions() & ios_base::badbit) {
679 this->setstate(__state);
684 template <class _CharT, class _Traits>
685 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::get(char_type* __s, streamsize __n, char_type __dlm) {
686 ios_base::iostate __state = ios_base::goodbit;
688 sentry __sen(*this, true);
691 # if _LIBCPP_HAS_EXCEPTIONS
694 while (__gc_ < __n - 1) {
695 int_type __i = this->rdbuf()->sgetc();
696 if (traits_type::eq_int_type(__i, traits_type::eof())) {
697 __state |= ios_base::eofbit;
700 char_type __ch = traits_type::to_char_type(__i);
701 if (traits_type::eq(__ch, __dlm))
705 this->rdbuf()->sbumpc();
708 __state |= ios_base::failbit;
709 # if _LIBCPP_HAS_EXCEPTIONS
711 __state |= ios_base::badbit;
712 this->__setstate_nothrow(__state);
713 if (this->exceptions() & ios_base::badbit) {
721 __state |= ios_base::failbit;
726 this->setstate(__state);
733 template <class _CharT, class _Traits>
734 basic_istream<_CharT, _Traits>&
735 basic_istream<_CharT, _Traits>::get(basic_streambuf<char_type, traits_type>& __sb, char_type __dlm) {
736 ios_base::iostate __state = ios_base::goodbit;
738 sentry __sen(*this, true);
740 # if _LIBCPP_HAS_EXCEPTIONS
742 # endif // _LIBCPP_HAS_EXCEPTIONS
744 typename traits_type::int_type __i = this->rdbuf()->sgetc();
745 if (traits_type::eq_int_type(__i, traits_type::eof())) {
746 __state |= ios_base::eofbit;
749 char_type __ch = traits_type::to_char_type(__i);
750 if (traits_type::eq(__ch, __dlm))
752 if (traits_type::eq_int_type(__sb.sputc(__ch), traits_type::eof()))
755 this->rdbuf()->sbumpc();
757 # if _LIBCPP_HAS_EXCEPTIONS
759 __state |= ios_base::badbit;
760 // according to the spec, exceptions here are caught but not rethrown
762 # endif // _LIBCPP_HAS_EXCEPTIONS
764 __state |= ios_base::failbit;
765 this->setstate(__state);
770 template <class _CharT, class _Traits>
771 basic_istream<_CharT, _Traits>&
772 basic_istream<_CharT, _Traits>::getline(char_type* __s, streamsize __n, char_type __dlm) {
773 ios_base::iostate __state = ios_base::goodbit;
775 sentry __sen(*this, true);
777 # if _LIBCPP_HAS_EXCEPTIONS
779 # endif // _LIBCPP_HAS_EXCEPTIONS
781 typename traits_type::int_type __i = this->rdbuf()->sgetc();
782 if (traits_type::eq_int_type(__i, traits_type::eof())) {
783 __state |= ios_base::eofbit;
786 char_type __ch = traits_type::to_char_type(__i);
787 if (traits_type::eq(__ch, __dlm)) {
788 this->rdbuf()->sbumpc();
792 if (__gc_ >= __n - 1) {
793 __state |= ios_base::failbit;
797 this->rdbuf()->sbumpc();
800 # if _LIBCPP_HAS_EXCEPTIONS
802 __state |= ios_base::badbit;
803 this->__setstate_nothrow(__state);
804 if (this->exceptions() & ios_base::badbit) {
808 __state |= ios_base::failbit;
812 # endif // _LIBCPP_HAS_EXCEPTIONS
817 __state |= ios_base::failbit;
818 this->setstate(__state);
822 template <class _CharT, class _Traits>
823 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::ignore(streamsize __n, int_type __dlm) {
824 ios_base::iostate __state = ios_base::goodbit;
826 sentry __sen(*this, true);
828 # if _LIBCPP_HAS_EXCEPTIONS
830 # endif // _LIBCPP_HAS_EXCEPTIONS
831 if (__n == numeric_limits<streamsize>::max()) {
833 typename traits_type::int_type __i = this->rdbuf()->sbumpc();
834 if (traits_type::eq_int_type(__i, traits_type::eof())) {
835 __state |= ios_base::eofbit;
839 if (traits_type::eq_int_type(__i, __dlm))
843 while (__gc_ < __n) {
844 typename traits_type::int_type __i = this->rdbuf()->sbumpc();
845 if (traits_type::eq_int_type(__i, traits_type::eof())) {
846 __state |= ios_base::eofbit;
850 if (traits_type::eq_int_type(__i, __dlm))
854 # if _LIBCPP_HAS_EXCEPTIONS
856 __state |= ios_base::badbit;
857 this->__setstate_nothrow(__state);
858 if (this->exceptions() & ios_base::badbit) {
862 # endif // _LIBCPP_HAS_EXCEPTIONS
863 this->setstate(__state);
868 template <class _CharT, class _Traits>
869 typename basic_istream<_CharT, _Traits>::int_type basic_istream<_CharT, _Traits>::peek() {
870 ios_base::iostate __state = ios_base::goodbit;
872 int_type __r = traits_type::eof();
873 sentry __sen(*this, true);
875 # if _LIBCPP_HAS_EXCEPTIONS
877 # endif // _LIBCPP_HAS_EXCEPTIONS
878 __r = this->rdbuf()->sgetc();
879 if (traits_type::eq_int_type(__r, traits_type::eof()))
880 __state |= ios_base::eofbit;
881 # if _LIBCPP_HAS_EXCEPTIONS
883 __state |= ios_base::badbit;
884 this->__setstate_nothrow(__state);
885 if (this->exceptions() & ios_base::badbit) {
889 # endif // _LIBCPP_HAS_EXCEPTIONS
890 this->setstate(__state);
895 template <class _CharT, class _Traits>
896 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::read(char_type* __s, streamsize __n) {
897 ios_base::iostate __state = ios_base::goodbit;
899 sentry __sen(*this, true);
901 # if _LIBCPP_HAS_EXCEPTIONS
903 # endif // _LIBCPP_HAS_EXCEPTIONS
904 __gc_ = this->rdbuf()->sgetn(__s, __n);
906 __state |= ios_base::failbit | ios_base::eofbit;
907 # if _LIBCPP_HAS_EXCEPTIONS
909 __state |= ios_base::badbit;
910 this->__setstate_nothrow(__state);
911 if (this->exceptions() & ios_base::badbit) {
915 # endif // _LIBCPP_HAS_EXCEPTIONS
917 __state |= ios_base::failbit;
919 this->setstate(__state);
923 template <class _CharT, class _Traits>
924 streamsize basic_istream<_CharT, _Traits>::readsome(char_type* __s, streamsize __n) {
925 ios_base::iostate __state = ios_base::goodbit;
927 sentry __sen(*this, true);
929 # if _LIBCPP_HAS_EXCEPTIONS
931 # endif // _LIBCPP_HAS_EXCEPTIONS
932 streamsize __c = this->rdbuf()->in_avail();
935 __state |= ios_base::eofbit;
940 __n = std::min(__c, __n);
941 __gc_ = this->rdbuf()->sgetn(__s, __n);
943 __state |= ios_base::failbit | ios_base::eofbit;
946 # if _LIBCPP_HAS_EXCEPTIONS
948 __state |= ios_base::badbit;
949 this->__setstate_nothrow(__state);
950 if (this->exceptions() & ios_base::badbit) {
954 # endif // _LIBCPP_HAS_EXCEPTIONS
956 __state |= ios_base::failbit;
958 this->setstate(__state);
962 template <class _CharT, class _Traits>
963 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::putback(char_type __c) {
964 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
966 this->clear(__state);
967 sentry __sen(*this, true);
969 # if _LIBCPP_HAS_EXCEPTIONS
971 # endif // _LIBCPP_HAS_EXCEPTIONS
972 if (this->rdbuf() == nullptr || this->rdbuf()->sputbackc(__c) == traits_type::eof())
973 __state |= ios_base::badbit;
974 # if _LIBCPP_HAS_EXCEPTIONS
976 __state |= ios_base::badbit;
977 this->__setstate_nothrow(__state);
978 if (this->exceptions() & ios_base::badbit) {
982 # endif // _LIBCPP_HAS_EXCEPTIONS
984 __state |= ios_base::failbit;
986 this->setstate(__state);
990 template <class _CharT, class _Traits>
991 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::unget() {
992 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
994 this->clear(__state);
995 sentry __sen(*this, true);
997 # if _LIBCPP_HAS_EXCEPTIONS
999 # endif // _LIBCPP_HAS_EXCEPTIONS
1000 if (this->rdbuf() == nullptr || this->rdbuf()->sungetc() == traits_type::eof())
1001 __state |= ios_base::badbit;
1002 # if _LIBCPP_HAS_EXCEPTIONS
1004 __state |= ios_base::badbit;
1005 this->__setstate_nothrow(__state);
1006 if (this->exceptions() & ios_base::badbit) {
1010 # endif // _LIBCPP_HAS_EXCEPTIONS
1012 __state |= ios_base::failbit;
1014 this->setstate(__state);
1018 template <class _CharT, class _Traits>
1019 int basic_istream<_CharT, _Traits>::sync() {
1020 ios_base::iostate __state = ios_base::goodbit;
1021 sentry __sen(*this, true);
1022 if (this->rdbuf() == nullptr)
1027 # if _LIBCPP_HAS_EXCEPTIONS
1029 # endif // _LIBCPP_HAS_EXCEPTIONS
1030 if (this->rdbuf()->pubsync() == -1) {
1031 __state |= ios_base::badbit;
1034 # if _LIBCPP_HAS_EXCEPTIONS
1036 __state |= ios_base::badbit;
1037 this->__setstate_nothrow(__state);
1038 if (this->exceptions() & ios_base::badbit) {
1042 # endif // _LIBCPP_HAS_EXCEPTIONS
1043 this->setstate(__state);
1048 template <class _CharT, class _Traits>
1049 typename basic_istream<_CharT, _Traits>::pos_type basic_istream<_CharT, _Traits>::tellg() {
1050 ios_base::iostate __state = ios_base::goodbit;
1052 sentry __sen(*this, true);
1054 # if _LIBCPP_HAS_EXCEPTIONS
1056 # endif // _LIBCPP_HAS_EXCEPTIONS
1057 __r = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in);
1058 # if _LIBCPP_HAS_EXCEPTIONS
1060 __state |= ios_base::badbit;
1061 this->__setstate_nothrow(__state);
1062 if (this->exceptions() & ios_base::badbit) {
1066 # endif // _LIBCPP_HAS_EXCEPTIONS
1067 this->setstate(__state);
1072 template <class _CharT, class _Traits>
1073 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::seekg(pos_type __pos) {
1074 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
1075 this->clear(__state);
1076 sentry __sen(*this, true);
1078 # if _LIBCPP_HAS_EXCEPTIONS
1080 # endif // _LIBCPP_HAS_EXCEPTIONS
1081 if (this->rdbuf()->pubseekpos(__pos, ios_base::in) == pos_type(-1))
1082 __state |= ios_base::failbit;
1083 # if _LIBCPP_HAS_EXCEPTIONS
1085 __state |= ios_base::badbit;
1086 this->__setstate_nothrow(__state);
1087 if (this->exceptions() & ios_base::badbit) {
1091 # endif // _LIBCPP_HAS_EXCEPTIONS
1092 this->setstate(__state);
1097 template <class _CharT, class _Traits>
1098 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::seekg(off_type __off, ios_base::seekdir __dir) {
1099 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
1100 this->clear(__state);
1101 sentry __sen(*this, true);
1103 # if _LIBCPP_HAS_EXCEPTIONS
1105 # endif // _LIBCPP_HAS_EXCEPTIONS
1106 if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::in) == pos_type(-1))
1107 __state |= ios_base::failbit;
1108 # if _LIBCPP_HAS_EXCEPTIONS
1110 __state |= ios_base::badbit;
1111 this->__setstate_nothrow(__state);
1112 if (this->exceptions() & ios_base::badbit) {
1116 # endif // _LIBCPP_HAS_EXCEPTIONS
1117 this->setstate(__state);
1122 template <class _CharT, class _Traits>
1123 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& ws(basic_istream<_CharT, _Traits>& __is) {
1124 ios_base::iostate __state = ios_base::goodbit;
1125 typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
1127 # if _LIBCPP_HAS_EXCEPTIONS
1129 # endif // _LIBCPP_HAS_EXCEPTIONS
1130 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
1132 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
1133 if (_Traits::eq_int_type(__i, _Traits::eof())) {
1134 __state |= ios_base::eofbit;
1137 if (!__ct.is(__ct.space, _Traits::to_char_type(__i)))
1139 __is.rdbuf()->sbumpc();
1141 # if _LIBCPP_HAS_EXCEPTIONS
1143 __state |= ios_base::badbit;
1144 __is.__setstate_nothrow(__state);
1145 if (__is.exceptions() & ios_base::badbit) {
1149 # endif // _LIBCPP_HAS_EXCEPTIONS
1150 __is.setstate(__state);
1155 template <class _Stream, class _Tp, class = void>
1156 struct __is_istreamable : false_type {};
1158 template <class _Stream, class _Tp>
1159 struct __is_istreamable<_Stream, _Tp, decltype(std::declval<_Stream>() >> std::declval<_Tp>(), void())> : true_type {};
1161 template <class _Stream,
1163 __enable_if_t< _And<is_base_of<ios_base, _Stream>, __is_istreamable<_Stream&, _Tp&&> >::value, int> = 0>
1164 _LIBCPP_HIDE_FROM_ABI _Stream&& operator>>(_Stream&& __is, _Tp&& __x) {
1165 __is >> std::forward<_Tp>(__x);
1166 return std::move(__is);
1169 template <class _CharT, class _Traits>
1170 class _LIBCPP_TEMPLATE_VIS basic_iostream
1171 : public basic_istream<_CharT, _Traits>,
1172 public basic_ostream<_CharT, _Traits> {
1175 typedef _CharT char_type;
1176 typedef _Traits traits_type;
1177 typedef typename traits_type::int_type int_type;
1178 typedef typename traits_type::pos_type pos_type;
1179 typedef typename traits_type::off_type off_type;
1181 // constructor/destructor
1182 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 explicit basic_iostream(basic_streambuf<char_type, traits_type>* __sb)
1183 : basic_istream<_CharT, _Traits>(__sb) {}
1185 ~basic_iostream() override;
1188 inline _LIBCPP_HIDE_FROM_ABI basic_iostream(basic_iostream&& __rhs);
1191 inline _LIBCPP_HIDE_FROM_ABI basic_iostream& operator=(basic_iostream&& __rhs);
1193 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void swap(basic_iostream& __rhs) {
1194 basic_istream<char_type, traits_type>::swap(__rhs);
1198 template <class _CharT, class _Traits>
1199 basic_iostream<_CharT, _Traits>::basic_iostream(basic_iostream&& __rhs)
1200 : basic_istream<_CharT, _Traits>(std::move(__rhs)) {}
1202 template <class _CharT, class _Traits>
1203 basic_iostream<_CharT, _Traits>& basic_iostream<_CharT, _Traits>::operator=(basic_iostream&& __rhs) {
1208 template <class _CharT, class _Traits>
1209 basic_iostream<_CharT, _Traits>::~basic_iostream() {}
1211 template <class _CharT, class _Traits, class _Allocator>
1212 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1213 operator>>(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str) {
1214 ios_base::iostate __state = ios_base::goodbit;
1215 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
1217 # if _LIBCPP_HAS_EXCEPTIONS
1221 using _Size = typename basic_string<_CharT, _Traits, _Allocator>::size_type;
1222 streamsize const __width = __is.width();
1223 _Size const __max_size = __str.max_size();
1228 __n = std::__to_unsigned_like(__width) < __max_size ? static_cast<_Size>(__width) : __max_size;
1232 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
1234 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
1235 if (_Traits::eq_int_type(__i, _Traits::eof())) {
1236 __state |= ios_base::eofbit;
1239 _CharT __ch = _Traits::to_char_type(__i);
1240 if (__ct.is(__ct.space, __ch))
1242 __str.push_back(__ch);
1244 __is.rdbuf()->sbumpc();
1248 __state |= ios_base::failbit;
1249 # if _LIBCPP_HAS_EXCEPTIONS
1251 __state |= ios_base::badbit;
1252 __is.__setstate_nothrow(__state);
1253 if (__is.exceptions() & ios_base::badbit) {
1258 __is.setstate(__state);
1263 template <class _CharT, class _Traits, class _Allocator>
1264 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1265 getline(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm) {
1266 ios_base::iostate __state = ios_base::goodbit;
1267 typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
1269 # if _LIBCPP_HAS_EXCEPTIONS
1273 streamsize __extr = 0;
1275 typename _Traits::int_type __i = __is.rdbuf()->sbumpc();
1276 if (_Traits::eq_int_type(__i, _Traits::eof())) {
1277 __state |= ios_base::eofbit;
1281 _CharT __ch = _Traits::to_char_type(__i);
1282 if (_Traits::eq(__ch, __dlm))
1284 __str.push_back(__ch);
1285 if (__str.size() == __str.max_size()) {
1286 __state |= ios_base::failbit;
1291 __state |= ios_base::failbit;
1292 # if _LIBCPP_HAS_EXCEPTIONS
1294 __state |= ios_base::badbit;
1295 __is.__setstate_nothrow(__state);
1296 if (__is.exceptions() & ios_base::badbit) {
1301 __is.setstate(__state);
1306 template <class _CharT, class _Traits, class _Allocator>
1307 inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1308 getline(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str) {
1309 return std::getline(__is, __str, __is.widen('\n'));
1312 template <class _CharT, class _Traits, class _Allocator>
1313 inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1314 getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm) {
1315 return std::getline(__is, __str, __dlm);
1318 template <class _CharT, class _Traits, class _Allocator>
1319 inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1320 getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Allocator>& __str) {
1321 return std::getline(__is, __str, __is.widen('\n'));
1324 template <class _CharT, class _Traits, size_t _Size>
1325 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1326 operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x) {
1327 ios_base::iostate __state = ios_base::goodbit;
1328 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
1330 # if _LIBCPP_HAS_EXCEPTIONS
1333 basic_string<_CharT, _Traits> __str;
1334 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
1336 _CharT __zero = __ct.widen('0');
1337 _CharT __one = __ct.widen('1');
1338 while (__c != _Size) {
1339 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
1340 if (_Traits::eq_int_type(__i, _Traits::eof())) {
1341 __state |= ios_base::eofbit;
1344 _CharT __ch = _Traits::to_char_type(__i);
1345 if (!_Traits::eq(__ch, __zero) && !_Traits::eq(__ch, __one))
1347 __str.push_back(__ch);
1349 __is.rdbuf()->sbumpc();
1351 __x = bitset<_Size>(__str);
1352 if (_Size > 0 && __c == 0)
1353 __state |= ios_base::failbit;
1354 # if _LIBCPP_HAS_EXCEPTIONS
1356 __state |= ios_base::badbit;
1357 __is.__setstate_nothrow(__state);
1358 if (__is.exceptions() & ios_base::badbit) {
1363 __is.setstate(__state);
1368 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istream<char>;
1369 # if _LIBCPP_HAS_WIDE_CHARACTERS
1370 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istream<wchar_t>;
1372 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_iostream<char>;
1374 _LIBCPP_END_NAMESPACE_STD
1376 # endif // _LIBCPP_HAS_LOCALIZATION
1378 # if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1379 # include <concepts>
1382 # include <type_traits>
1387 #endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
1389 #endif // _LIBCPP_ISTREAM