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 // <ostream> 13 14 // template <class charT, class traits = char_traits<charT> > 15 // class basic_ostream; 16 17 // template <class charT, class traits, class T> 18 // basic_ostream<charT, traits>& 19 // operator<<(basic_ostream<charT, traits>&& os, const T& x); 20 21 #include <ostream> 22 #include <cassert> 23 24 25 template <class CharT> 26 class testbuf 27 : public std::basic_streambuf<CharT> 28 { 29 typedef std::basic_streambuf<CharT> base; 30 std::basic_string<CharT> str_; 31 public: 32 testbuf() 33 { 34 } 35 36 std::basic_string<CharT> str() const 37 {return std::basic_string<CharT>(base::pbase(), base::pptr());} 38 39 protected: 40 41 virtual typename base::int_type 42 overflow(typename base::int_type ch = base::traits_type::eof()) 43 { 44 if (ch != base::traits_type::eof()) 45 { 46 int n = static_cast<int>(str_.size()); 47 str_.push_back(static_cast<CharT>(ch)); 48 str_.resize(str_.capacity()); 49 base::setp(const_cast<CharT*>(str_.data()), 50 const_cast<CharT*>(str_.data() + str_.size())); 51 base::pbump(n+1); 52 } 53 return ch; 54 } 55 }; 56 57 58 int main() 59 { 60 { 61 testbuf<char> sb; 62 std::ostream(&sb) << "testing..."; 63 assert(sb.str() == "testing..."); 64 } 65 { 66 testbuf<wchar_t> sb; 67 std::wostream(&sb) << L"123"; 68 assert(sb.str() == L"123"); 69 } 70 } 71