brintos

brintos / llvm-project-archived public Read only

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