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 // <sstream> 11 12 // template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > 13 // class basic_stringstream 14 15 // explicit basic_stringstream(const basic_string<charT,traits,Allocator>& str, 16 // ios_base::openmode which = ios_base::out|ios_base::in); 17 18 #include <sstream> 19 #include <cassert> 20 21 template<typename T> 22 struct NoDefaultAllocator : std::allocator<T> 23 { 24 template<typename U> struct rebind { using other = NoDefaultAllocator<U>; }; 25 NoDefaultAllocator(int id_) : id(id_) { } 26 template<typename U> NoDefaultAllocator(const NoDefaultAllocator<U>& a) : id(a.id) { } 27 int id; 28 }; 29 30 31 int main() 32 { 33 { 34 std::stringstream ss(" 123 456 "); 35 assert(ss.rdbuf() != 0); 36 assert(ss.good()); 37 assert(ss.str() == " 123 456 "); 38 int i = 0; 39 ss >> i; 40 assert(i == 123); 41 ss >> i; 42 assert(i == 456); 43 ss << i << ' ' << 123; 44 assert(ss.str() == "456 1236 "); 45 } 46 { 47 std::wstringstream ss(L" 123 456 "); 48 assert(ss.rdbuf() != 0); 49 assert(ss.good()); 50 assert(ss.str() == L" 123 456 "); 51 int i = 0; 52 ss >> i; 53 assert(i == 123); 54 ss >> i; 55 assert(i == 456); 56 ss << i << ' ' << 123; 57 assert(ss.str() == L"456 1236 "); 58 } 59 { // This is https://bugs.llvm.org/show_bug.cgi?id=33727 60 typedef std::basic_string <char, std::char_traits<char>, NoDefaultAllocator<char> > S; 61 typedef std::basic_stringbuf<char, std::char_traits<char>, NoDefaultAllocator<char> > SB; 62 63 S s(NoDefaultAllocator<char>(1)); 64 SB sb(s); 65 // This test is not required by the standard, but *where else* could it get the allocator? 66 assert(sb.str().get_allocator() == s.get_allocator()); 67 } 68 } 69