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