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