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