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