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