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 // <istream>
10
11 // template <class Stream, class T>
12 // Stream&& operator>>(Stream&& is, T&& x);
13
14 #include <istream>
15 #include <sstream>
16 #include <cassert>
17
18 #include "test_macros.h"
19
20 template <class CharT>
21 struct testbuf
22 : public std::basic_streambuf<CharT>
23 {
24 typedef std::basic_string<CharT> string_type;
25 typedef std::basic_streambuf<CharT> base;
26 private:
27 string_type str_;
28 public:
29
testbuftestbuf30 testbuf() {}
testbuftestbuf31 testbuf(const string_type& str)
32 : str_(str)
33 {
34 base::setg(const_cast<CharT*>(str_.data()),
35 const_cast<CharT*>(str_.data()),
36 const_cast<CharT*>(str_.data()) + str_.size());
37 }
38
ebacktestbuf39 CharT* eback() const {return base::eback();}
gptrtestbuf40 CharT* gptr() const {return base::gptr();}
egptrtestbuf41 CharT* egptr() const {return base::egptr();}
42 };
43
44 struct Int {
45 int value;
46 template <class CharT>
operator >>(std::basic_istream<CharT> & is,Int & self)47 friend void operator>>(std::basic_istream<CharT>& is, Int& self) {
48 is >> self.value;
49 }
50 };
51
52 struct A { };
53 bool called = false;
operator >>(std::istream &,A &&)54 void operator>>(std::istream&, A&&) { called = true; }
55
main(int,char **)56 int main(int, char**)
57 {
58 {
59 testbuf<char> sb(" 123");
60 Int i = {0};
61 std::istream is(&sb);
62 std::istream&& result = (std::move(is) >> i);
63 assert(&result == &is);
64 assert(i.value == 123);
65 }
66 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
67 {
68 testbuf<wchar_t> sb(L" 123");
69 Int i = {0};
70 std::wistream is(&sb);
71 std::wistream&& result = (std::move(is) >> i);
72 assert(&result == &is);
73 assert(i.value == 123);
74 }
75 #endif
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