1 // Copyright (C) 2008-2024 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-do run { target c++11 } }
22 #include <testsuite_hooks.h>
28 std::ostringstream() << i
;
29 std::string
result ("1742");
31 std::istringstream(result
) >> i2
;
35 struct X
{ bool as_rvalue
; };
37 void operator>>(std::istream
&, X
& x
) { x
.as_rvalue
= false; }
38 void operator>>(std::istream
&, X
&& x
) { x
.as_rvalue
= true; }
40 // LWG 2328 Rvalue stream extraction should use perfect forwarding
45 std::istringstream is
;
46 auto&& ref1
= (std::move(is
) >> x
);
47 VERIFY( &ref1
== &is
);
48 VERIFY( x
.as_rvalue
== false );
49 auto&& ref2
= (std::move(is
) >> std::move(x
));
50 VERIFY( &ref2
== &is
);
51 VERIFY( x
.as_rvalue
== true );
54 #if __cplusplus <= 201703L
55 std::istringstream("x") >> &arr
[0];
57 std::istringstream("x") >> arr
;
58 VERIFY( std::string(arr
) == "x" );
61 // LWG 1203 More useful rvalue stream insertion
66 std::string result
= (std::ostringstream() << "i = " << i
).str();
67 VERIFY( result
== "i = 1203" );
69 std::ostringstream os
;
70 std::ostringstream
&& ros
= std::move(os
) << result
;
71 VERIFY( &ros
== &os
);
72 VERIFY( ros
.str() == result
);
75 std::stringstream
&& rss
= std::move(ss
) << result
;
76 VERIFY( &rss
== &ss
);
77 VERIFY( rss
.str() == result
);
79 std::istringstream
is("first second third");
80 std::istringstream
&& ris
= std::move(is
) >> result
;
81 VERIFY( &ris
== &is
);
82 VERIFY( result
== "first" );
84 std::stringstream
ss2("fourth fifth sixth");
85 std::stringstream
&& rss2
= std::move(ss2
) >> result
;
86 VERIFY( &rss2
== &ss2
);
87 VERIFY( result
== "fourth" );
90 struct A
{ friend void operator<<(std::ios_base
&, A
) { } };
92 struct O
: private std::ios_base
{ friend void operator<<(O
&, int) { } };
94 template<typename Ostream
, typename T
, typename
= void>
99 template<typename
> using void_t
= void;
101 template<typename Ostream
, typename T
>
103 = decltype(std::declval
<Ostream
>() << std::declval
<const T
&>());
105 template<typename Ostream
, typename T
>
106 struct is_insertable
<Ostream
, T
, void_t
<insert_result
<Ostream
, T
>>>
110 // LWG 1203 negative tests
114 static_assert( is_insertable
<std::ios_base
&, A
>::value
,
115 "valid using the friend operator<<" );
116 static_assert( !is_insertable
<std::ios_base
&&, A
>::value
,
117 "ill-formed because ios_base is not derived from ios_base" );
119 static_assert( is_insertable
<O
&, int>::value
,
120 "valid using the friend operator<<" );
121 static_assert( !is_insertable
<O
&&, int>::value
,
122 "ill-formed because O is not publicly derived from ios_base" );