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++03
10 
11 // <ostream>
12 
13 // template <class Stream, class T>
14 // Stream&& operator<<(Stream&& os, const T& x);
15 
16 #include <ostream>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 
21 
22 template <class CharT>
23 class testbuf
24     : public std::basic_streambuf<CharT>
25 {
26     typedef std::basic_streambuf<CharT> base;
27     std::basic_string<CharT> str_;
28 public:
testbuf()29     testbuf()
30     {
31     }
32 
str() const33     std::basic_string<CharT> str() const
34         {return std::basic_string<CharT>(base::pbase(), base::pptr());}
35 
36 protected:
37 
38     virtual typename base::int_type
overflow(typename base::int_type ch=base::traits_type::eof ())39         overflow(typename base::int_type ch = base::traits_type::eof())
40         {
41             if (ch != base::traits_type::eof())
42             {
43                 int n = static_cast<int>(str_.size());
44                 str_.push_back(static_cast<CharT>(ch));
45                 str_.resize(str_.capacity());
46                 base::setp(const_cast<CharT*>(str_.data()),
47                            const_cast<CharT*>(str_.data() + str_.size()));
48                 base::pbump(n+1);
49             }
50             return ch;
51         }
52 };
53 
54 struct Int {
55     int value;
56     template <class CharT>
operator <<(std::basic_ostream<CharT> & os,Int const & self)57     friend void operator<<(std::basic_ostream<CharT>& os, Int const& self) {
58         os << self.value;
59     }
60 };
61 
main(int,char **)62 int main(int, char**)
63 {
64     {
65         testbuf<char> sb;
66         std::ostream os(&sb);
67         Int const i = {123};
68         std::ostream&& result = (std::move(os) << i);
69         assert(&result == &os);
70         assert(sb.str() == "123");
71     }
72     {
73         testbuf<wchar_t> sb;
74         std::wostream os(&sb);
75         Int const i = {123};
76         std::wostream&& result = (std::move(os) << i);
77         assert(&result == &os);
78         assert(sb.str() == L"123");
79     }
80 
81     return 0;
82 }
83