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