brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.1 KiB · 23ad80e Raw
87 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// <istream>10 11// template <class Stream, class T>12// Stream&& operator>>(Stream&& is, T&& x);13 14#include <istream>15#include <sstream>16#include <cassert>17 18#include "test_macros.h"19 20template <class CharT>21struct testbuf22    : public std::basic_streambuf<CharT>23{24    typedef std::basic_string<CharT> string_type;25    typedef std::basic_streambuf<CharT> base;26private:27    string_type str_;28public:29 30    testbuf() {}31    testbuf(const string_type& str)32        : str_(str)33    {34        base::setg(const_cast<CharT*>(str_.data()),35                   const_cast<CharT*>(str_.data()),36                   const_cast<CharT*>(str_.data()) + str_.size());37    }38 39    CharT* eback() const {return base::eback();}40    CharT* gptr() const {return base::gptr();}41    CharT* egptr() const {return base::egptr();}42};43 44struct Int {45    int value;46    template <class CharT>47    friend void operator>>(std::basic_istream<CharT>& is, Int& self) {48        is >> self.value;49    }50};51 52struct A { };53bool called = false;54void operator>>(std::istream&, A&&) { called = true; }55 56int main(int, char**)57{58    {59        testbuf<char> sb("   123");60        Int i = {0};61        std::istream is(&sb);62        std::istream&& result = (std::move(is) >> i);63        assert(&result == &is);64        assert(i.value == 123);65    }66#ifndef TEST_HAS_NO_WIDE_CHARACTERS67    {68        testbuf<wchar_t> sb(L"   123");69        Int i = {0};70        std::wistream is(&sb);71        std::wistream&& result = (std::move(is) >> i);72        assert(&result == &is);73        assert(i.value == 123);74    }75#endif76    {77        // test perfect forwarding78        assert(called == false);79        std::istringstream ss;80        std::istringstream&& result = (std::move(ss) >> A());81        assert(&result == &ss);82        assert(called);83    }84 85    return 0;86}87