• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
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
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_ISTREAM
11#define _LIBCPP_ISTREAM
12
13/*
14    istream synopsis
15
16template <class charT, class traits = char_traits<charT> >
17class basic_istream
18    : virtual public basic_ios<charT,traits>
19{
20public:
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;
27
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();
32
33    // 27.7.1.1.2 Assign/swap:
34    basic_istream& operator=(basic_istream&& rhs);
35    void swap(basic_istream& rhs);
36
37    // 27.7.1.1.3 Prefix/suffix:
38    class sentry;
39
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);
59
60    // 27.7.1.3 Unformatted input:
61    streamsize gcount() const;
62    int_type get();
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);
68
69    basic_istream& getline(char_type* s, streamsize n);
70    basic_istream& getline(char_type* s, streamsize n, char_type delim);
71
72    basic_istream& ignore(streamsize n = 1, int_type delim = traits_type::eof());
73    int_type peek();
74    basic_istream& read (char_type* s, streamsize n);
75    streamsize readsome(char_type* s, streamsize n);
76
77    basic_istream& putback(char_type c);
78    basic_istream& unget();
79    int sync();
80
81    pos_type tellg();
82    basic_istream& seekg(pos_type);
83    basic_istream& seekg(off_type, ios_base::seekdir);
84protected:
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);
91};
92
93// 27.7.1.2.3 character extraction templates:
94template<class charT, class traits>
95  basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&, charT&);
96
97template<class traits>
98  basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, unsigned char&);
99
100template<class traits>
101  basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, signed char&);
102
103template<class charT, class traits>
104  basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&, charT*);
105
106template<class traits>
107  basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, unsigned char*);
108
109template<class traits>
110  basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, signed char*);
111
112template <class charT, class traits>
113  void
114  swap(basic_istream<charT, traits>& x, basic_istream<charT, traits>& y);
115
116typedef basic_istream<char> istream;
117typedef basic_istream<wchar_t> wistream;
118
119template <class charT, class traits = char_traits<charT> >
120class basic_iostream :
121    public basic_istream<charT,traits>,
122    public basic_ostream<charT,traits>
123{
124public:
125    // types:
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;
131
132    // constructor/destructor
133    explicit basic_iostream(basic_streambuf<char_type, traits_type>* sb);
134    basic_iostream(basic_iostream&& rhs);
135    virtual ~basic_iostream();
136
137    // assign/swap
138    basic_iostream& operator=(basic_iostream&& rhs);
139    void swap(basic_iostream& rhs);
140};
141
142template <class charT, class traits>
143  void
144  swap(basic_iostream<charT, traits>& x, basic_iostream<charT, traits>& y);
145
146typedef basic_iostream<char> iostream;
147typedef basic_iostream<wchar_t> wiostream;
148
149template <class charT, class traits>
150  basic_istream<charT,traits>&
151  ws(basic_istream<charT,traits>& is);
152
153// rvalue stream extraction
154template <class Stream, class T>
155  Stream&& operator>>(Stream&& is, T&& x);
156
157}  // std
158
159*/
160
161#include <__assert> // all public C++ headers provide the assertion handler
162#include <__config>
163#include <__iterator/istreambuf_iterator.h>
164#include <__type_traits/conjunction.h>
165#include <__type_traits/enable_if.h>
166#include <__type_traits/is_base_of.h>
167#include <__utility/declval.h>
168#include <__utility/forward.h>
169#include <ostream>
170#include <version>
171
172#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
173#  pragma GCC system_header
174#endif
175
176_LIBCPP_PUSH_MACROS
177#include <__undef_macros>
178
179
180_LIBCPP_BEGIN_NAMESPACE_STD
181
182template <class _CharT, class _Traits>
183class _LIBCPP_TEMPLATE_VIS basic_istream
184    : virtual public basic_ios<_CharT, _Traits>
185{
186    streamsize __gc_;
187public:
188    // types (inherited from basic_ios (27.5.4)):
189    typedef _CharT                         char_type;
190    typedef _Traits                        traits_type;
191    typedef typename traits_type::int_type int_type;
192    typedef typename traits_type::pos_type pos_type;
193    typedef typename traits_type::off_type off_type;
194
195    // 27.7.1.1.1 Constructor/destructor:
196    inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
197    explicit basic_istream(basic_streambuf<char_type, traits_type>* __sb) : __gc_(0)
198    { this->init(__sb); }
199    ~basic_istream() override;
200protected:
201    inline _LIBCPP_INLINE_VISIBILITY
202    basic_istream(basic_istream&& __rhs);
203
204    // 27.7.1.1.2 Assign/swap:
205    inline _LIBCPP_INLINE_VISIBILITY
206    basic_istream& operator=(basic_istream&& __rhs);
207
208    inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
209    void swap(basic_istream& __rhs) {
210      _VSTD::swap(__gc_, __rhs.__gc_);
211      basic_ios<char_type, traits_type>::swap(__rhs);
212    }
213
214    basic_istream           (const basic_istream& __rhs) = delete;
215    basic_istream& operator=(const basic_istream& __rhs) = delete;
216public:
217
218    // 27.7.1.1.3 Prefix/suffix:
219    class _LIBCPP_TEMPLATE_VIS sentry;
220
221    // 27.7.1.2 Formatted input:
222    inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
223    basic_istream& operator>>(basic_istream& (*__pf)(basic_istream&))
224    { return __pf(*this); }
225
226    inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
227    basic_istream& operator>>(basic_ios<char_type, traits_type>&
228                              (*__pf)(basic_ios<char_type, traits_type>&))
229    { __pf(*this); return *this; }
230
231    inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
232    basic_istream& operator>>(ios_base& (*__pf)(ios_base&))
233    { __pf(*this); return *this; }
234
235    basic_istream& operator>>(basic_streambuf<char_type, traits_type>* __sb);
236    basic_istream& operator>>(bool& __n);
237    basic_istream& operator>>(short& __n);
238    basic_istream& operator>>(unsigned short& __n);
239    basic_istream& operator>>(int& __n);
240    basic_istream& operator>>(unsigned int& __n);
241    basic_istream& operator>>(long& __n);
242    basic_istream& operator>>(unsigned long& __n);
243    basic_istream& operator>>(long long& __n);
244    basic_istream& operator>>(unsigned long long& __n);
245    basic_istream& operator>>(float& __f);
246    basic_istream& operator>>(double& __f);
247    basic_istream& operator>>(long double& __f);
248    basic_istream& operator>>(void*& __p);
249
250    // 27.7.1.3 Unformatted input:
251    _LIBCPP_INLINE_VISIBILITY
252    streamsize gcount() const {return __gc_;}
253    int_type get();
254
255    inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
256    basic_istream& get(char_type& __c) {
257      int_type __ch = get();
258      if (__ch != traits_type::eof())
259        __c = traits_type::to_char_type(__ch);
260      return *this;
261    }
262
263    inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
264    basic_istream& get(char_type* __s, streamsize __n)
265    { return get(__s, __n, this->widen('\n')); }
266
267    basic_istream& get(char_type* __s, streamsize __n, char_type __dlm);
268
269    inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
270    basic_istream& get(basic_streambuf<char_type, traits_type>& __sb)
271    { return get(__sb, this->widen('\n')); }
272
273    basic_istream& get(basic_streambuf<char_type, traits_type>& __sb, char_type __dlm);
274
275    inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
276    basic_istream& getline(char_type* __s, streamsize __n)
277    { return getline(__s, __n, this->widen('\n')); }
278
279    basic_istream& getline(char_type* __s, streamsize __n, char_type __dlm);
280
281    basic_istream& ignore(streamsize __n = 1, int_type __dlm = traits_type::eof());
282    int_type peek();
283    basic_istream& read (char_type* __s, streamsize __n);
284    streamsize readsome(char_type* __s, streamsize __n);
285
286    basic_istream& putback(char_type __c);
287    basic_istream& unget();
288    int sync();
289
290    pos_type tellg();
291    basic_istream& seekg(pos_type __pos);
292    basic_istream& seekg(off_type __off, ios_base::seekdir __dir);
293};
294
295template <class _CharT, class _Traits>
296class _LIBCPP_TEMPLATE_VIS basic_istream<_CharT, _Traits>::sentry
297{
298    bool __ok_;
299
300public:
301    explicit sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false);
302//    ~sentry() = default;
303
304    _LIBCPP_INLINE_VISIBILITY
305    explicit operator bool() const {return __ok_;}
306
307    sentry(const sentry&) = delete;
308    sentry& operator=(const sentry&) = delete;
309};
310
311template <class _CharT, class _Traits>
312basic_istream<_CharT, _Traits>::sentry::sentry(basic_istream<_CharT, _Traits>& __is,
313                                               bool __noskipws)
314    : __ok_(false)
315{
316    if (__is.good())
317    {
318        if (__is.tie())
319            __is.tie()->flush();
320        if (!__noskipws && (__is.flags() & ios_base::skipws))
321        {
322            typedef istreambuf_iterator<_CharT, _Traits> _Ip;
323            const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
324            _Ip __i(__is);
325            _Ip __eof;
326            for (; __i != __eof; ++__i)
327                if (!__ct.is(__ct.space, *__i))
328                    break;
329            if (__i == __eof)
330                __is.setstate(ios_base::failbit | ios_base::eofbit);
331        }
332        __ok_ = __is.good();
333    }
334    else
335        __is.setstate(ios_base::failbit);
336}
337
338template <class _CharT, class _Traits>
339basic_istream<_CharT, _Traits>::basic_istream(basic_istream&& __rhs)
340    : __gc_(__rhs.__gc_)
341{
342    __rhs.__gc_ = 0;
343    this->move(__rhs);
344}
345
346template <class _CharT, class _Traits>
347basic_istream<_CharT, _Traits>&
348basic_istream<_CharT, _Traits>::operator=(basic_istream&& __rhs)
349{
350    swap(__rhs);
351    return *this;
352}
353
354template <class _CharT, class _Traits>
355basic_istream<_CharT, _Traits>::~basic_istream()
356{
357}
358
359template <class _Tp, class _CharT, class _Traits>
360_LIBCPP_INLINE_VISIBILITY
361basic_istream<_CharT, _Traits>&
362__input_arithmetic(basic_istream<_CharT, _Traits>& __is, _Tp& __n) {
363    ios_base::iostate __state = ios_base::goodbit;
364    typename basic_istream<_CharT, _Traits>::sentry __s(__is);
365    if (__s)
366    {
367#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
368        try
369        {
370#endif // _LIBCPP_HAS_NO_EXCEPTIONS
371            typedef istreambuf_iterator<_CharT, _Traits> _Ip;
372            typedef num_get<_CharT, _Ip> _Fp;
373            std::use_facet<_Fp>(__is.getloc()).get(_Ip(__is), _Ip(), __is, __state, __n);
374#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
375        }
376        catch (...)
377        {
378            __state |= ios_base::badbit;
379            __is.__setstate_nothrow(__state);
380            if (__is.exceptions() & ios_base::badbit)
381            {
382                throw;
383            }
384        }
385#endif
386        __is.setstate(__state);
387    }
388    return __is;
389}
390
391template <class _CharT, class _Traits>
392basic_istream<_CharT, _Traits>&
393basic_istream<_CharT, _Traits>::operator>>(unsigned short& __n)
394{
395    return _VSTD::__input_arithmetic<unsigned short>(*this, __n);
396}
397
398template <class _CharT, class _Traits>
399basic_istream<_CharT, _Traits>&
400basic_istream<_CharT, _Traits>::operator>>(unsigned int& __n)
401{
402    return _VSTD::__input_arithmetic<unsigned int>(*this, __n);
403}
404
405template <class _CharT, class _Traits>
406basic_istream<_CharT, _Traits>&
407basic_istream<_CharT, _Traits>::operator>>(long& __n)
408{
409    return _VSTD::__input_arithmetic<long>(*this, __n);
410}
411
412template <class _CharT, class _Traits>
413basic_istream<_CharT, _Traits>&
414basic_istream<_CharT, _Traits>::operator>>(unsigned long& __n)
415{
416    return _VSTD::__input_arithmetic<unsigned long>(*this, __n);
417}
418
419template <class _CharT, class _Traits>
420basic_istream<_CharT, _Traits>&
421basic_istream<_CharT, _Traits>::operator>>(long long& __n)
422{
423    return _VSTD::__input_arithmetic<long long>(*this, __n);
424}
425
426template <class _CharT, class _Traits>
427basic_istream<_CharT, _Traits>&
428basic_istream<_CharT, _Traits>::operator>>(unsigned long long& __n)
429{
430    return _VSTD::__input_arithmetic<unsigned long long>(*this, __n);
431}
432
433template <class _CharT, class _Traits>
434basic_istream<_CharT, _Traits>&
435basic_istream<_CharT, _Traits>::operator>>(float& __n)
436{
437    return _VSTD::__input_arithmetic<float>(*this, __n);
438}
439
440template <class _CharT, class _Traits>
441basic_istream<_CharT, _Traits>&
442basic_istream<_CharT, _Traits>::operator>>(double& __n)
443{
444    return _VSTD::__input_arithmetic<double>(*this, __n);
445}
446
447template <class _CharT, class _Traits>
448basic_istream<_CharT, _Traits>&
449basic_istream<_CharT, _Traits>::operator>>(long double& __n)
450{
451    return _VSTD::__input_arithmetic<long double>(*this, __n);
452}
453
454template <class _CharT, class _Traits>
455basic_istream<_CharT, _Traits>&
456basic_istream<_CharT, _Traits>::operator>>(bool& __n)
457{
458    return _VSTD::__input_arithmetic<bool>(*this, __n);
459}
460
461template <class _CharT, class _Traits>
462basic_istream<_CharT, _Traits>&
463basic_istream<_CharT, _Traits>::operator>>(void*& __n)
464{
465    return _VSTD::__input_arithmetic<void*>(*this, __n);
466}
467
468template <class _Tp, class _CharT, class _Traits>
469_LIBCPP_INLINE_VISIBILITY
470basic_istream<_CharT, _Traits>&
471__input_arithmetic_with_numeric_limits(basic_istream<_CharT, _Traits>& __is, _Tp& __n) {
472    ios_base::iostate __state = ios_base::goodbit;
473    typename basic_istream<_CharT, _Traits>::sentry __s(__is);
474    if (__s)
475    {
476#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
477        try
478        {
479#endif // _LIBCPP_HAS_NO_EXCEPTIONS
480            typedef istreambuf_iterator<_CharT, _Traits> _Ip;
481            typedef num_get<_CharT, _Ip> _Fp;
482            long __temp;
483            std::use_facet<_Fp>(__is.getloc()).get(_Ip(__is), _Ip(), __is, __state, __temp);
484            if (__temp < numeric_limits<_Tp>::min())
485            {
486                __state |= ios_base::failbit;
487                __n = numeric_limits<_Tp>::min();
488            }
489            else if (__temp > numeric_limits<_Tp>::max())
490            {
491                __state |= ios_base::failbit;
492                __n = numeric_limits<_Tp>::max();
493            }
494            else
495            {
496                __n = static_cast<_Tp>(__temp);
497            }
498#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
499        }
500        catch (...)
501        {
502            __state |= ios_base::badbit;
503            __is.__setstate_nothrow(__state);
504            if (__is.exceptions() & ios_base::badbit)
505            {
506                throw;
507            }
508        }
509#endif // _LIBCPP_HAS_NO_EXCEPTIONS
510        __is.setstate(__state);
511    }
512    return __is;
513}
514
515template <class _CharT, class _Traits>
516basic_istream<_CharT, _Traits>&
517basic_istream<_CharT, _Traits>::operator>>(short& __n)
518{
519    return _VSTD::__input_arithmetic_with_numeric_limits<short>(*this, __n);
520}
521
522template <class _CharT, class _Traits>
523basic_istream<_CharT, _Traits>&
524basic_istream<_CharT, _Traits>::operator>>(int& __n)
525{
526    return _VSTD::__input_arithmetic_with_numeric_limits<int>(*this, __n);
527}
528
529template<class _CharT, class _Traits>
530_LIBCPP_INLINE_VISIBILITY
531basic_istream<_CharT, _Traits>&
532__input_c_string(basic_istream<_CharT, _Traits>& __is, _CharT* __p, size_t __n)
533{
534    ios_base::iostate __state = ios_base::goodbit;
535    typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
536    if (__sen)
537    {
538#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
539        try
540        {
541#endif
542            _CharT* __s = __p;
543            const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
544            while (__s != __p + (__n-1))
545            {
546                typename _Traits::int_type __i = __is.rdbuf()->sgetc();
547                if (_Traits::eq_int_type(__i, _Traits::eof()))
548                {
549                   __state |= ios_base::eofbit;
550                   break;
551                }
552                _CharT __ch = _Traits::to_char_type(__i);
553                if (__ct.is(__ct.space, __ch))
554                    break;
555                *__s++ = __ch;
556                 __is.rdbuf()->sbumpc();
557            }
558            *__s = _CharT();
559            __is.width(0);
560            if (__s == __p)
561               __state |= ios_base::failbit;
562#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
563        }
564        catch (...)
565        {
566            __state |= ios_base::badbit;
567            __is.__setstate_nothrow(__state);
568            if (__is.exceptions() & ios_base::badbit)
569            {
570                throw;
571            }
572        }
573#endif
574        __is.setstate(__state);
575    }
576    return __is;
577}
578
579#if _LIBCPP_STD_VER >= 20
580
581template<class _CharT, class _Traits, size_t _Np>
582inline _LIBCPP_INLINE_VISIBILITY
583basic_istream<_CharT, _Traits>&
584operator>>(basic_istream<_CharT, _Traits>& __is, _CharT (&__buf)[_Np])
585{
586    size_t __n = _Np;
587    if (__is.width() > 0)
588        __n = _VSTD::min(size_t(__is.width()), _Np);
589    return _VSTD::__input_c_string(__is, __buf, __n);
590}
591
592template<class _Traits, size_t _Np>
593inline _LIBCPP_INLINE_VISIBILITY
594basic_istream<char, _Traits>&
595operator>>(basic_istream<char, _Traits>& __is, unsigned char (&__buf)[_Np])
596{
597    return __is >> (char(&)[_Np])__buf;
598}
599
600template<class _Traits, size_t _Np>
601inline _LIBCPP_INLINE_VISIBILITY
602basic_istream<char, _Traits>&
603operator>>(basic_istream<char, _Traits>& __is, signed char (&__buf)[_Np])
604{
605    return __is >> (char(&)[_Np])__buf;
606}
607
608#else
609
610template<class _CharT, class _Traits>
611inline _LIBCPP_INLINE_VISIBILITY
612basic_istream<_CharT, _Traits>&
613operator>>(basic_istream<_CharT, _Traits>& __is, _CharT* __s)
614{
615    streamsize __n = __is.width();
616    if (__n <= 0)
617        __n = numeric_limits<streamsize>::max() / sizeof(_CharT) - 1;
618    return _VSTD::__input_c_string(__is, __s, size_t(__n));
619}
620
621template<class _Traits>
622inline _LIBCPP_INLINE_VISIBILITY
623basic_istream<char, _Traits>&
624operator>>(basic_istream<char, _Traits>& __is, unsigned char* __s)
625{
626    return __is >> (char*)__s;
627}
628
629template<class _Traits>
630inline _LIBCPP_INLINE_VISIBILITY
631basic_istream<char, _Traits>&
632operator>>(basic_istream<char, _Traits>& __is, signed char* __s)
633{
634    return __is >> (char*)__s;
635}
636
637#endif // _LIBCPP_STD_VER >= 20
638
639template<class _CharT, class _Traits>
640_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
641operator>>(basic_istream<_CharT, _Traits>& __is, _CharT& __c)
642{
643    ios_base::iostate __state = ios_base::goodbit;
644    typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
645    if (__sen)
646    {
647#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
648        try
649        {
650#endif
651            typename _Traits::int_type __i = __is.rdbuf()->sbumpc();
652            if (_Traits::eq_int_type(__i, _Traits::eof()))
653                __state |= ios_base::eofbit | ios_base::failbit;
654            else
655                __c = _Traits::to_char_type(__i);
656#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
657        }
658        catch (...)
659        {
660            __state |= ios_base::badbit;
661            __is.__setstate_nothrow(__state);
662            if (__is.exceptions() & ios_base::badbit)
663            {
664                throw;
665            }
666        }
667#endif
668        __is.setstate(__state);
669    }
670    return __is;
671}
672
673template<class _Traits>
674inline _LIBCPP_INLINE_VISIBILITY
675basic_istream<char, _Traits>&
676operator>>(basic_istream<char, _Traits>& __is, unsigned char& __c)
677{
678    return __is >> (char&)__c;
679}
680
681template<class _Traits>
682inline _LIBCPP_INLINE_VISIBILITY
683basic_istream<char, _Traits>&
684operator>>(basic_istream<char, _Traits>& __is, signed char& __c)
685{
686    return __is >> (char&)__c;
687}
688
689template<class _CharT, class _Traits>
690basic_istream<_CharT, _Traits>&
691basic_istream<_CharT, _Traits>::operator>>(basic_streambuf<char_type, traits_type>* __sb)
692{
693    ios_base::iostate __state = ios_base::goodbit;
694    __gc_ = 0;
695    sentry __s(*this, true);
696    if (__s)
697    {
698        if (__sb)
699        {
700#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
701            try
702            {
703#endif // _LIBCPP_HAS_NO_EXCEPTIONS
704                while (true)
705                {
706                    typename traits_type::int_type __i = this->rdbuf()->sgetc();
707                    if (traits_type::eq_int_type(__i, _Traits::eof()))
708                    {
709                       __state |= ios_base::eofbit;
710                       break;
711                    }
712                    if (traits_type::eq_int_type(
713                            __sb->sputc(traits_type::to_char_type(__i)),
714                            traits_type::eof()))
715                        break;
716                    ++__gc_;
717                    this->rdbuf()->sbumpc();
718                }
719                if (__gc_ == 0)
720                   __state |= ios_base::failbit;
721#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
722            }
723            catch (...)
724            {
725                __state |= ios_base::badbit;
726                if (__gc_ == 0)
727                    __state |= ios_base::failbit;
728
729                this->__setstate_nothrow(__state);
730                if (this->exceptions() & ios_base::failbit || this->exceptions() & ios_base::badbit)
731                {
732                    throw;
733                }
734            }
735#endif // _LIBCPP_HAS_NO_EXCEPTIONS
736        }
737        else
738        {
739            __state |= ios_base::failbit;
740        }
741        this->setstate(__state);
742    }
743    return *this;
744}
745
746template<class _CharT, class _Traits>
747typename basic_istream<_CharT, _Traits>::int_type
748basic_istream<_CharT, _Traits>::get()
749{
750    ios_base::iostate __state = ios_base::goodbit;
751    __gc_ = 0;
752    int_type __r = traits_type::eof();
753    sentry __s(*this, true);
754    if (__s)
755    {
756#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
757        try
758        {
759#endif
760            __r = this->rdbuf()->sbumpc();
761            if (traits_type::eq_int_type(__r, traits_type::eof()))
762               __state |= ios_base::failbit | ios_base::eofbit;
763            else
764                __gc_ = 1;
765#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
766        }
767        catch (...)
768        {
769            this->__setstate_nothrow(this->rdstate() | ios_base::badbit);
770            if (this->exceptions() & ios_base::badbit)
771            {
772                throw;
773            }
774        }
775#endif
776        this->setstate(__state);
777    }
778    return __r;
779}
780
781template<class _CharT, class _Traits>
782basic_istream<_CharT, _Traits>&
783basic_istream<_CharT, _Traits>::get(char_type* __s, streamsize __n, char_type __dlm)
784{
785    ios_base::iostate __state = ios_base::goodbit;
786    __gc_ = 0;
787    sentry __sen(*this, true);
788    if (__sen)
789    {
790        if (__n > 0)
791        {
792#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
793            try
794            {
795#endif
796                while (__gc_ < __n-1)
797                {
798                    int_type __i = this->rdbuf()->sgetc();
799                    if (traits_type::eq_int_type(__i, traits_type::eof()))
800                    {
801                       __state |= ios_base::eofbit;
802                       break;
803                    }
804                    char_type __ch = traits_type::to_char_type(__i);
805                    if (traits_type::eq(__ch, __dlm))
806                        break;
807                    *__s++ = __ch;
808                    ++__gc_;
809                     this->rdbuf()->sbumpc();
810                }
811                if (__gc_ == 0)
812                   __state |= ios_base::failbit;
813#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
814            }
815            catch (...)
816            {
817                __state |= ios_base::badbit;
818                this->__setstate_nothrow(__state);
819                if (this->exceptions() & ios_base::badbit)
820                {
821                    if (__n > 0)
822                        *__s = char_type();
823                    throw;
824                }
825            }
826#endif
827        }
828        else
829        {
830            __state |= ios_base::failbit;
831        }
832
833        if (__n > 0)
834            *__s = char_type();
835        this->setstate(__state);
836    }
837    if (__n > 0)
838        *__s = char_type();
839    return *this;
840}
841
842template<class _CharT, class _Traits>
843basic_istream<_CharT, _Traits>&
844basic_istream<_CharT, _Traits>::get(basic_streambuf<char_type, traits_type>& __sb,
845                                    char_type __dlm)
846{
847    ios_base::iostate __state = ios_base::goodbit;
848    __gc_ = 0;
849    sentry __sen(*this, true);
850    if (__sen)
851    {
852#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
853        try
854        {
855#endif // _LIBCPP_HAS_NO_EXCEPTIONS
856            while (true)
857            {
858                typename traits_type::int_type __i = this->rdbuf()->sgetc();
859                if (traits_type::eq_int_type(__i, traits_type::eof()))
860                {
861                   __state |= ios_base::eofbit;
862                   break;
863                }
864                char_type __ch = traits_type::to_char_type(__i);
865                if (traits_type::eq(__ch, __dlm))
866                    break;
867                if (traits_type::eq_int_type(__sb.sputc(__ch), traits_type::eof()))
868                    break;
869                ++__gc_;
870                this->rdbuf()->sbumpc();
871            }
872#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
873        }
874        catch (...)
875        {
876            __state |= ios_base::badbit;
877            // according to the spec, exceptions here are caught but not rethrown
878        }
879#endif // _LIBCPP_HAS_NO_EXCEPTIONS
880        if (__gc_ == 0)
881           __state |= ios_base::failbit;
882        this->setstate(__state);
883    }
884    return *this;
885}
886
887template<class _CharT, class _Traits>
888basic_istream<_CharT, _Traits>&
889basic_istream<_CharT, _Traits>::getline(char_type* __s, streamsize __n, char_type __dlm)
890{
891    ios_base::iostate __state = ios_base::goodbit;
892    __gc_ = 0;
893    sentry __sen(*this, true);
894    if (__sen)
895    {
896#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
897        try
898        {
899#endif // _LIBCPP_HAS_NO_EXCEPTIONS
900            while (true)
901            {
902                typename traits_type::int_type __i = this->rdbuf()->sgetc();
903                if (traits_type::eq_int_type(__i, traits_type::eof()))
904                {
905                   __state |= ios_base::eofbit;
906                   break;
907                }
908                char_type __ch = traits_type::to_char_type(__i);
909                if (traits_type::eq(__ch, __dlm))
910                {
911                    this->rdbuf()->sbumpc();
912                    ++__gc_;
913                    break;
914                }
915                if (__gc_ >= __n-1)
916                {
917                    __state |= ios_base::failbit;
918                    break;
919                }
920                *__s++ = __ch;
921                this->rdbuf()->sbumpc();
922                ++__gc_;
923            }
924#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
925        }
926        catch (...)
927        {
928            __state |= ios_base::badbit;
929            this->__setstate_nothrow(__state);
930            if (this->exceptions() & ios_base::badbit)
931            {
932                if (__n > 0)
933                    *__s = char_type();
934                if (__gc_ == 0)
935                    __state |= ios_base::failbit;
936                throw;
937            }
938        }
939#endif // _LIBCPP_HAS_NO_EXCEPTIONS
940    }
941    if (__n > 0)
942        *__s = char_type();
943    if (__gc_ == 0)
944        __state |= ios_base::failbit;
945    this->setstate(__state);
946    return *this;
947}
948
949template<class _CharT, class _Traits>
950basic_istream<_CharT, _Traits>&
951basic_istream<_CharT, _Traits>::ignore(streamsize __n, int_type __dlm)
952{
953    ios_base::iostate __state = ios_base::goodbit;
954    __gc_ = 0;
955    sentry __sen(*this, true);
956    if (__sen)
957    {
958#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
959        try
960        {
961#endif // _LIBCPP_HAS_NO_EXCEPTIONS
962            if (__n == numeric_limits<streamsize>::max())
963            {
964                while (true)
965                {
966                    typename traits_type::int_type __i = this->rdbuf()->sbumpc();
967                    if (traits_type::eq_int_type(__i, traits_type::eof()))
968                    {
969                       __state |= ios_base::eofbit;
970                       break;
971                    }
972                    ++__gc_;
973                    if (traits_type::eq_int_type(__i, __dlm))
974                        break;
975                }
976            }
977            else
978            {
979                while (__gc_ < __n)
980                {
981                    typename traits_type::int_type __i = this->rdbuf()->sbumpc();
982                    if (traits_type::eq_int_type(__i, traits_type::eof()))
983                    {
984                       __state |= ios_base::eofbit;
985                       break;
986                    }
987                    ++__gc_;
988                    if (traits_type::eq_int_type(__i, __dlm))
989                        break;
990                }
991            }
992#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
993        }
994        catch (...)
995        {
996            __state |= ios_base::badbit;
997            this->__setstate_nothrow(__state);
998            if (this->exceptions() & ios_base::badbit)
999            {
1000                throw;
1001            }
1002        }
1003#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1004        this->setstate(__state);
1005    }
1006    return *this;
1007}
1008
1009template<class _CharT, class _Traits>
1010typename basic_istream<_CharT, _Traits>::int_type
1011basic_istream<_CharT, _Traits>::peek()
1012{
1013    ios_base::iostate __state = ios_base::goodbit;
1014    __gc_ = 0;
1015    int_type __r = traits_type::eof();
1016    sentry __sen(*this, true);
1017    if (__sen)
1018    {
1019#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1020        try
1021        {
1022#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1023            __r = this->rdbuf()->sgetc();
1024            if (traits_type::eq_int_type(__r, traits_type::eof()))
1025                __state |= ios_base::eofbit;
1026#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1027        }
1028        catch (...)
1029        {
1030            __state |= ios_base::badbit;
1031            this->__setstate_nothrow(__state);
1032            if (this->exceptions() & ios_base::badbit)
1033            {
1034                throw;
1035            }
1036        }
1037#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1038        this->setstate(__state);
1039    }
1040    return __r;
1041}
1042
1043template<class _CharT, class _Traits>
1044basic_istream<_CharT, _Traits>&
1045basic_istream<_CharT, _Traits>::read(char_type* __s, streamsize __n)
1046{
1047    ios_base::iostate __state = ios_base::goodbit;
1048    __gc_ = 0;
1049    sentry __sen(*this, true);
1050    if (__sen)
1051    {
1052#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1053        try
1054        {
1055#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1056            __gc_ = this->rdbuf()->sgetn(__s, __n);
1057            if (__gc_ != __n)
1058                __state |= ios_base::failbit | ios_base::eofbit;
1059#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1060        }
1061        catch (...)
1062        {
1063            __state |= ios_base::badbit;
1064            this->__setstate_nothrow(__state);
1065            if (this->exceptions() & ios_base::badbit)
1066            {
1067                throw;
1068            }
1069        }
1070#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1071    }
1072    else
1073    {
1074        __state |= ios_base::failbit;
1075    }
1076    this->setstate(__state);
1077    return *this;
1078}
1079
1080template<class _CharT, class _Traits>
1081streamsize
1082basic_istream<_CharT, _Traits>::readsome(char_type* __s, streamsize __n)
1083{
1084    ios_base::iostate __state = ios_base::goodbit;
1085    __gc_ = 0;
1086    sentry __sen(*this, true);
1087    if (__sen)
1088    {
1089#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1090        try
1091        {
1092#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1093            streamsize __c = this->rdbuf()->in_avail();
1094            switch (__c)
1095            {
1096            case -1:
1097                __state |= ios_base::eofbit;
1098                break;
1099            case 0:
1100                break;
1101            default:
1102                __n = _VSTD::min(__c, __n);
1103                __gc_ = this->rdbuf()->sgetn(__s, __n);
1104                if (__gc_ != __n)
1105                    __state |= ios_base::failbit | ios_base::eofbit;
1106                break;
1107            }
1108#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1109        }
1110        catch (...)
1111        {
1112            __state |= ios_base::badbit;
1113            this->__setstate_nothrow(__state);
1114            if (this->exceptions() & ios_base::badbit)
1115            {
1116                throw;
1117            }
1118        }
1119#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1120    }
1121    else
1122    {
1123        __state |= ios_base::failbit;
1124    }
1125    this->setstate(__state);
1126    return __gc_;
1127}
1128
1129template<class _CharT, class _Traits>
1130basic_istream<_CharT, _Traits>&
1131basic_istream<_CharT, _Traits>::putback(char_type __c)
1132{
1133    ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
1134    __gc_ = 0;
1135    this->clear(__state);
1136    sentry __sen(*this, true);
1137    if (__sen)
1138    {
1139#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1140        try
1141        {
1142#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1143            if (this->rdbuf() == nullptr || this->rdbuf()->sputbackc(__c) == traits_type::eof())
1144                __state |= ios_base::badbit;
1145#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1146        }
1147        catch (...)
1148        {
1149            __state |= ios_base::badbit;
1150            this->__setstate_nothrow(__state);
1151            if (this->exceptions() & ios_base::badbit)
1152            {
1153                throw;
1154            }
1155        }
1156#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1157    }
1158    else
1159    {
1160        __state |= ios_base::failbit;
1161    }
1162    this->setstate(__state);
1163    return *this;
1164}
1165
1166template<class _CharT, class _Traits>
1167basic_istream<_CharT, _Traits>&
1168basic_istream<_CharT, _Traits>::unget()
1169{
1170    ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
1171    __gc_ = 0;
1172    this->clear(__state);
1173    sentry __sen(*this, true);
1174    if (__sen)
1175    {
1176#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1177        try
1178        {
1179#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1180            if (this->rdbuf() == nullptr || this->rdbuf()->sungetc() == traits_type::eof())
1181                __state |= ios_base::badbit;
1182#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1183        }
1184        catch (...)
1185        {
1186            __state |= ios_base::badbit;
1187            this->__setstate_nothrow(__state);
1188            if (this->exceptions() & ios_base::badbit)
1189            {
1190                throw;
1191            }
1192        }
1193#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1194    }
1195    else
1196    {
1197        __state |= ios_base::failbit;
1198    }
1199    this->setstate(__state);
1200    return *this;
1201}
1202
1203template<class _CharT, class _Traits>
1204int
1205basic_istream<_CharT, _Traits>::sync()
1206{
1207    ios_base::iostate __state = ios_base::goodbit;
1208    int __r = 0;
1209    sentry __sen(*this, true);
1210    if (__sen)
1211    {
1212#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1213        try
1214        {
1215#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1216            if (this->rdbuf() == nullptr)
1217                return -1;
1218            if (this->rdbuf()->pubsync() == -1)
1219            {
1220                __state |= ios_base::badbit;
1221                return -1;
1222            }
1223#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1224        }
1225        catch (...)
1226        {
1227            __state |= ios_base::badbit;
1228            this->__setstate_nothrow(__state);
1229            if (this->exceptions() & ios_base::badbit)
1230            {
1231                throw;
1232            }
1233        }
1234#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1235        this->setstate(__state);
1236    }
1237    return __r;
1238}
1239
1240template<class _CharT, class _Traits>
1241typename basic_istream<_CharT, _Traits>::pos_type
1242basic_istream<_CharT, _Traits>::tellg()
1243{
1244    ios_base::iostate __state = ios_base::goodbit;
1245    pos_type __r(-1);
1246    sentry __sen(*this, true);
1247    if (__sen)
1248    {
1249#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1250        try
1251        {
1252#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1253        __r = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in);
1254#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1255        }
1256        catch (...)
1257        {
1258            __state |= ios_base::badbit;
1259            this->__setstate_nothrow(__state);
1260            if (this->exceptions() & ios_base::badbit)
1261            {
1262                throw;
1263            }
1264        }
1265#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1266        this->setstate(__state);
1267    }
1268    return __r;
1269}
1270
1271template<class _CharT, class _Traits>
1272basic_istream<_CharT, _Traits>&
1273basic_istream<_CharT, _Traits>::seekg(pos_type __pos)
1274{
1275    ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
1276    this->clear(__state);
1277    sentry __sen(*this, true);
1278    if (__sen)
1279    {
1280#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1281        try
1282        {
1283#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1284            if (this->rdbuf()->pubseekpos(__pos, ios_base::in) == pos_type(-1))
1285                __state |= ios_base::failbit;
1286#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1287        }
1288        catch (...)
1289        {
1290            __state |= ios_base::badbit;
1291            this->__setstate_nothrow(__state);
1292            if (this->exceptions() & ios_base::badbit)
1293            {
1294                throw;
1295            }
1296        }
1297#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1298        this->setstate(__state);
1299    }
1300    return *this;
1301}
1302
1303template<class _CharT, class _Traits>
1304basic_istream<_CharT, _Traits>&
1305basic_istream<_CharT, _Traits>::seekg(off_type __off, ios_base::seekdir __dir)
1306{
1307    ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
1308    this->clear(__state);
1309    sentry __sen(*this, true);
1310    if (__sen)
1311    {
1312#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1313        try
1314        {
1315#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1316            if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::in) == pos_type(-1))
1317                __state |= ios_base::failbit;
1318#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1319        }
1320        catch (...)
1321        {
1322            __state |= ios_base::badbit;
1323            this->__setstate_nothrow(__state);
1324            if (this->exceptions() & ios_base::badbit)
1325            {
1326                throw;
1327            }
1328        }
1329#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1330        this->setstate(__state);
1331    }
1332    return *this;
1333}
1334
1335template <class _CharT, class _Traits>
1336_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1337ws(basic_istream<_CharT, _Traits>& __is)
1338{
1339    ios_base::iostate __state = ios_base::goodbit;
1340    typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
1341    if (__sen)
1342    {
1343#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1344        try
1345        {
1346#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1347            const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
1348            while (true)
1349            {
1350                typename _Traits::int_type __i = __is.rdbuf()->sgetc();
1351                if (_Traits::eq_int_type(__i, _Traits::eof()))
1352                {
1353                   __state |= ios_base::eofbit;
1354                   break;
1355                }
1356                if (!__ct.is(__ct.space, _Traits::to_char_type(__i)))
1357                    break;
1358                __is.rdbuf()->sbumpc();
1359            }
1360#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1361        }
1362        catch (...)
1363        {
1364            __state |= ios_base::badbit;
1365            __is.__setstate_nothrow(__state);
1366            if (__is.exceptions() & ios_base::badbit)
1367            {
1368                throw;
1369            }
1370        }
1371#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1372        __is.setstate(__state);
1373    }
1374    return __is;
1375}
1376
1377template <class _Stream, class _Tp, class = void>
1378struct __is_istreamable : false_type { };
1379
1380template <class _Stream, class _Tp>
1381struct __is_istreamable<_Stream, _Tp, decltype(
1382    std::declval<_Stream>() >> std::declval<_Tp>(), void()
1383)> : true_type { };
1384
1385template <class _Stream, class _Tp, class = typename enable_if<
1386    _And<is_base_of<ios_base, _Stream>,
1387         __is_istreamable<_Stream&, _Tp&&> >::value
1388>::type>
1389_LIBCPP_INLINE_VISIBILITY
1390_Stream&& operator>>(_Stream&& __is, _Tp&& __x)
1391{
1392    __is >> _VSTD::forward<_Tp>(__x);
1393    return _VSTD::move(__is);
1394}
1395
1396template <class _CharT, class _Traits>
1397class _LIBCPP_TEMPLATE_VIS basic_iostream
1398    : public basic_istream<_CharT, _Traits>,
1399      public basic_ostream<_CharT, _Traits>
1400{
1401public:
1402    // types:
1403    typedef _CharT                         char_type;
1404    typedef _Traits                        traits_type;
1405    typedef typename traits_type::int_type int_type;
1406    typedef typename traits_type::pos_type pos_type;
1407    typedef typename traits_type::off_type off_type;
1408
1409    // constructor/destructor
1410    inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
1411    explicit basic_iostream(basic_streambuf<char_type, traits_type>* __sb)
1412      : basic_istream<_CharT, _Traits>(__sb)
1413    {}
1414
1415    ~basic_iostream() override;
1416protected:
1417    inline _LIBCPP_INLINE_VISIBILITY
1418    basic_iostream(basic_iostream&& __rhs);
1419
1420    // assign/swap
1421    inline _LIBCPP_INLINE_VISIBILITY
1422    basic_iostream& operator=(basic_iostream&& __rhs);
1423
1424    inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
1425    void swap(basic_iostream& __rhs)
1426    { basic_istream<char_type, traits_type>::swap(__rhs); }
1427};
1428
1429template <class _CharT, class _Traits>
1430basic_iostream<_CharT, _Traits>::basic_iostream(basic_iostream&& __rhs)
1431    : basic_istream<_CharT, _Traits>(_VSTD::move(__rhs))
1432{
1433}
1434
1435template <class _CharT, class _Traits>
1436basic_iostream<_CharT, _Traits>&
1437basic_iostream<_CharT, _Traits>::operator=(basic_iostream&& __rhs)
1438{
1439    swap(__rhs);
1440    return *this;
1441}
1442
1443template <class _CharT, class _Traits>
1444basic_iostream<_CharT, _Traits>::~basic_iostream()
1445{
1446}
1447
1448template<class _CharT, class _Traits, class _Allocator>
1449_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1450operator>>(basic_istream<_CharT, _Traits>& __is,
1451           basic_string<_CharT, _Traits, _Allocator>& __str)
1452{
1453    ios_base::iostate __state = ios_base::goodbit;
1454    typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
1455    if (__sen)
1456    {
1457#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1458        try
1459        {
1460#endif
1461            __str.clear();
1462            streamsize __n = __is.width();
1463            if (__n <= 0)
1464                __n = __str.max_size();
1465            if (__n <= 0)
1466                __n = numeric_limits<streamsize>::max();
1467            streamsize __c = 0;
1468            const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
1469            while (__c < __n)
1470            {
1471                typename _Traits::int_type __i = __is.rdbuf()->sgetc();
1472                if (_Traits::eq_int_type(__i, _Traits::eof()))
1473                {
1474                   __state |= ios_base::eofbit;
1475                   break;
1476                }
1477                _CharT __ch = _Traits::to_char_type(__i);
1478                if (__ct.is(__ct.space, __ch))
1479                    break;
1480                __str.push_back(__ch);
1481                ++__c;
1482                 __is.rdbuf()->sbumpc();
1483            }
1484            __is.width(0);
1485            if (__c == 0)
1486               __state |= ios_base::failbit;
1487#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1488        }
1489        catch (...)
1490        {
1491            __state |= ios_base::badbit;
1492            __is.__setstate_nothrow(__state);
1493            if (__is.exceptions() & ios_base::badbit)
1494            {
1495                throw;
1496            }
1497        }
1498#endif
1499        __is.setstate(__state);
1500    }
1501    return __is;
1502}
1503
1504template<class _CharT, class _Traits, class _Allocator>
1505_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1506getline(basic_istream<_CharT, _Traits>& __is,
1507        basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm)
1508{
1509    ios_base::iostate __state = ios_base::goodbit;
1510    typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
1511    if (__sen)
1512    {
1513#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1514        try
1515        {
1516#endif
1517            __str.clear();
1518            streamsize __extr = 0;
1519            while (true)
1520            {
1521                typename _Traits::int_type __i = __is.rdbuf()->sbumpc();
1522                if (_Traits::eq_int_type(__i, _Traits::eof()))
1523                {
1524                   __state |= ios_base::eofbit;
1525                   break;
1526                }
1527                ++__extr;
1528                _CharT __ch = _Traits::to_char_type(__i);
1529                if (_Traits::eq(__ch, __dlm))
1530                    break;
1531                __str.push_back(__ch);
1532                if (__str.size() == __str.max_size())
1533                {
1534                    __state |= ios_base::failbit;
1535                    break;
1536                }
1537            }
1538            if (__extr == 0)
1539               __state |= ios_base::failbit;
1540#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1541        }
1542        catch (...)
1543        {
1544            __state |= ios_base::badbit;
1545            __is.__setstate_nothrow(__state);
1546            if (__is.exceptions() & ios_base::badbit)
1547            {
1548                throw;
1549            }
1550        }
1551#endif
1552        __is.setstate(__state);
1553    }
1554    return __is;
1555}
1556
1557template<class _CharT, class _Traits, class _Allocator>
1558inline _LIBCPP_INLINE_VISIBILITY
1559basic_istream<_CharT, _Traits>&
1560getline(basic_istream<_CharT, _Traits>& __is,
1561        basic_string<_CharT, _Traits, _Allocator>& __str)
1562{
1563    return std::getline(__is, __str, __is.widen('\n'));
1564}
1565
1566template<class _CharT, class _Traits, class _Allocator>
1567inline _LIBCPP_INLINE_VISIBILITY
1568basic_istream<_CharT, _Traits>&
1569getline(basic_istream<_CharT, _Traits>&& __is,
1570        basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm)
1571{
1572    return std::getline(__is, __str, __dlm);
1573}
1574
1575template<class _CharT, class _Traits, class _Allocator>
1576inline _LIBCPP_INLINE_VISIBILITY
1577basic_istream<_CharT, _Traits>&
1578getline(basic_istream<_CharT, _Traits>&& __is,
1579        basic_string<_CharT, _Traits, _Allocator>& __str)
1580{
1581    return std::getline(__is, __str, __is.widen('\n'));
1582}
1583
1584template <class _CharT, class _Traits, size_t _Size>
1585_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1586operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x)
1587{
1588    ios_base::iostate __state = ios_base::goodbit;
1589    typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
1590    if (__sen)
1591    {
1592#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1593        try
1594        {
1595#endif
1596            basic_string<_CharT, _Traits> __str;
1597            const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
1598            size_t __c = 0;
1599            _CharT __zero = __ct.widen('0');
1600            _CharT __one = __ct.widen('1');
1601            while (__c != _Size)
1602            {
1603                typename _Traits::int_type __i = __is.rdbuf()->sgetc();
1604                if (_Traits::eq_int_type(__i, _Traits::eof()))
1605                {
1606                   __state |= ios_base::eofbit;
1607                   break;
1608                }
1609                _CharT __ch = _Traits::to_char_type(__i);
1610                if (!_Traits::eq(__ch, __zero) && !_Traits::eq(__ch, __one))
1611                    break;
1612                __str.push_back(__ch);
1613                ++__c;
1614                 __is.rdbuf()->sbumpc();
1615            }
1616            __x = bitset<_Size>(__str);
1617            if (_Size > 0 && __c == 0)
1618               __state |= ios_base::failbit;
1619#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1620        }
1621        catch (...)
1622        {
1623            __state |= ios_base::badbit;
1624            __is.__setstate_nothrow(__state);
1625            if (__is.exceptions() & ios_base::badbit)
1626            {
1627                throw;
1628            }
1629        }
1630#endif
1631        __is.setstate(__state);
1632    }
1633    return __is;
1634}
1635
1636extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istream<char>;
1637#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1638extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istream<wchar_t>;
1639#endif
1640extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_iostream<char>;
1641
1642_LIBCPP_END_NAMESPACE_STD
1643
1644#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1645#  include <concepts>
1646#  include <type_traits>
1647#endif
1648
1649_LIBCPP_POP_MACROS
1650
1651#endif // _LIBCPP_ISTREAM
1652