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