1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03
10 
11 // <istream>
12 
13 // template <class Stream, class T>
14 // Stream&& operator>>(Stream&& is, T&& x);
15 
16 #include <istream>
17 #include <sstream>
18 #include <cassert>
19 
20 #include "test_macros.h"
21 
22 template <class CharT>
23 struct testbuf
24     : public std::basic_streambuf<CharT>
25 {
26     typedef std::basic_string<CharT> string_type;
27     typedef std::basic_streambuf<CharT> base;
28 private:
29     string_type str_;
30 public:
31 
32     testbuf() {}
33     testbuf(const string_type& str)
34         : str_(str)
35     {
36         base::setg(const_cast<CharT*>(str_.data()),
37                    const_cast<CharT*>(str_.data()),
38                    const_cast<CharT*>(str_.data()) + str_.size());
39     }
40 
41     CharT* eback() const {return base::eback();}
42     CharT* gptr() const {return base::gptr();}
43     CharT* egptr() const {return base::egptr();}
44 };
45 
46 struct Int {
47     int value;
48     template <class CharT>
49     friend void operator>>(std::basic_istream<CharT>& is, Int& self) {
50         is >> self.value;
51     }
52 };
53 
54 struct A { };
55 bool called = false;
56 void operator>>(std::istream&, A&&) { called = true; }
57 
58 int main(int, char**)
59 {
60     {
61         testbuf<char> sb("   123");
62         Int i = {0};
63         std::istream is(&sb);
64         std::istream&& result = (std::move(is) >> i);
65         assert(&result == &is);
66         assert(i.value == 123);
67     }
68     {
69         testbuf<wchar_t> sb(L"   123");
70         Int i = {0};
71         std::wistream is(&sb);
72         std::wistream&& result = (std::move(is) >> i);
73         assert(&result == &is);
74         assert(i.value == 123);
75     }
76     {
77         // test perfect forwarding
78         assert(called == false);
79         std::istringstream ss;
80         std::istringstream&& result = (std::move(ss) >> A{});
81         assert(&result == &ss);
82         assert(called);
83     }
84 
85     return 0;
86 }
87