1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // UNSUPPORTED: c++98, c++03 11 12 // <istream> 13 14 // template <class charT, class traits, class T> 15 // basic_istream<charT, traits>& 16 // operator>>(basic_istream<charT, traits>&& is, T&& x); 17 18 #include <istream> 19 #include <sstream> 20 #include <cassert> 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 47 struct A{}; 48 bool called = false; 49 void operator>>(std::istream&, A&&){ called = true; } 50 51 int main() 52 { 53 { 54 testbuf<char> sb(" 123"); 55 int i = 0; 56 std::istream(&sb) >> i; 57 assert(i == 123); 58 } 59 { 60 testbuf<wchar_t> sb(L" 123"); 61 int i = 0; 62 std::wistream(&sb) >> i; 63 assert(i == 123); 64 } 65 { // test perfect forwarding 66 assert(called == false); 67 std::istringstream ss; 68 auto& out = (std::move(ss) >> A{}); 69 assert(&out == &ss); 70 assert(called); 71 } 72 } 73