1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
// Copyright (C) 2008-2022 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-do run { target c++11 } }
#include <sstream>
#include <string>
#include <testsuite_hooks.h>
void
test01()
{
int i = 1742;
std::ostringstream() << i;
std::string result ("1742");
int i2;
std::istringstream(result) >> i2;
VERIFY (i == i2);
}
struct X { bool as_rvalue; };
void operator>>(std::istream&, X& x) { x.as_rvalue = false; }
void operator>>(std::istream&, X&& x) { x.as_rvalue = true; }
// LWG 2328 Rvalue stream extraction should use perfect forwarding
void
test02()
{
X x;
std::istringstream is;
auto&& ref1 = (std::move(is) >> x);
VERIFY( &ref1 == &is );
VERIFY( x.as_rvalue == false );
auto&& ref2 = (std::move(is) >> std::move(x));
VERIFY( &ref2 == &is );
VERIFY( x.as_rvalue == true );
char arr[2];
#if __cplusplus <= 201703L
std::istringstream("x") >> &arr[0];
#endif
std::istringstream("x") >> arr;
VERIFY( std::string(arr) == "x" );
}
// LWG 1203 More useful rvalue stream insertion
void
test03()
{
int i = 1203;
std::string result = (std::ostringstream() << "i = " << i).str();
VERIFY( result == "i = 1203" );
std::ostringstream os;
std::ostringstream&& ros = std::move(os) << result;
VERIFY( &ros == &os );
VERIFY( ros.str() == result );
std::stringstream ss;
std::stringstream&& rss = std::move(ss) << result;
VERIFY( &rss == &ss );
VERIFY( rss.str() == result );
std::istringstream is("first second third");
std::istringstream&& ris = std::move(is) >> result;
VERIFY( &ris == &is );
VERIFY( result == "first" );
std::stringstream ss2("fourth fifth sixth");
std::stringstream&& rss2 = std::move(ss2) >> result;
VERIFY( &rss2 == &ss2 );
VERIFY( result == "fourth" );
}
struct A { friend void operator<<(std::ios_base&, A) { } };
struct O : private std::ios_base { friend void operator<<(O&, int) { } };
template<typename Ostream, typename T, typename = void>
struct is_insertable
: std::false_type
{ };
template<typename> using void_t = void;
template<typename Ostream, typename T>
using insert_result
= decltype(std::declval<Ostream>() << std::declval<const T&>());
template<typename Ostream, typename T>
struct is_insertable<Ostream, T, void_t<insert_result<Ostream, T>>>
: std::true_type
{ };
// LWG 1203 negative tests
void
test04()
{
static_assert( is_insertable<std::ios_base&, A>::value,
"valid using the friend operator<<" );
static_assert( !is_insertable<std::ios_base&&, A>::value,
"ill-formed because ios_base is not derived from ios_base" );
static_assert( is_insertable<O&, int>::value,
"valid using the friend operator<<" );
static_assert( !is_insertable<O&&, int>::value,
"ill-formed because O is not publicly derived from ios_base" );
}
int
main()
{
test01();
test02();
test03();
test04();
}
|