| 1 | // Copyright (C) 2008-2025 Free Software Foundation, Inc. |
| 2 | // |
| 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) |
| 7 | // any later version. |
| 8 | |
| 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. |
| 13 | |
| 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/>. |
| 17 | |
| 18 | // { dg-do run { target c++11 } } |
| 19 | |
| 20 | #include <sstream> |
| 21 | #include <string> |
| 22 | #include <testsuite_hooks.h> |
| 23 | |
| 24 | void |
| 25 | test01() |
| 26 | { |
| 27 | int i = 1742; |
| 28 | std::ostringstream() << i; |
| 29 | std::string result ("1742"); |
| 30 | int i2; |
| 31 | std::istringstream(result) >> i2; |
| 32 | VERIFY (i == i2); |
| 33 | } |
| 34 | |
| 35 | struct X { bool as_rvalue; }; |
| 36 | |
| 37 | void operator>>(std::istream&, X& x) { x.as_rvalue = false; } |
| 38 | void operator>>(std::istream&, X&& x) { x.as_rvalue = true; } |
| 39 | |
| 40 | // LWG 2328 Rvalue stream extraction should use perfect forwarding |
| 41 | void |
| 42 | test02() |
| 43 | { |
| 44 | X x; |
| 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 ); |
| 52 | |
| 53 | char arr[2]; |
| 54 | #if __cplusplus <= 201703L |
| 55 | std::istringstream("x") >> &arr[0]; |
| 56 | #endif |
| 57 | std::istringstream("x") >> arr; |
| 58 | VERIFY( std::string(arr) == "x" ); |
| 59 | } |
| 60 | |
| 61 | // LWG 1203 More useful rvalue stream insertion |
| 62 | void |
| 63 | test03() |
| 64 | { |
| 65 | int i = 1203; |
| 66 | std::string result = (std::ostringstream() << "i = " << i).str(); |
| 67 | VERIFY( result == "i = 1203" ); |
| 68 | |
| 69 | std::ostringstream os; |
| 70 | std::ostringstream&& ros = std::move(os) << result; |
| 71 | VERIFY( &ros == &os ); |
| 72 | VERIFY( ros.str() == result ); |
| 73 | |
| 74 | std::stringstream ss; |
| 75 | std::stringstream&& rss = std::move(ss) << result; |
| 76 | VERIFY( &rss == &ss ); |
| 77 | VERIFY( rss.str() == result ); |
| 78 | |
| 79 | std::istringstream is("first second third"); |
| 80 | std::istringstream&& ris = std::move(is) >> result; |
| 81 | VERIFY( &ris == &is ); |
| 82 | VERIFY( result == "first" ); |
| 83 | |
| 84 | std::stringstream ss2("fourth fifth sixth"); |
| 85 | std::stringstream&& rss2 = std::move(ss2) >> result; |
| 86 | VERIFY( &rss2 == &ss2 ); |
| 87 | VERIFY( result == "fourth" ); |
| 88 | } |
| 89 | |
| 90 | struct A { friend void operator<<(std::ios_base&, A) { } }; |
| 91 | |
| 92 | struct O : private std::ios_base { friend void operator<<(O&, int) { } }; |
| 93 | |
| 94 | template<typename Ostream, typename T, typename = void> |
| 95 | struct is_insertable |
| 96 | : std::false_type |
| 97 | { }; |
| 98 | |
| 99 | template<typename> using void_t = void; |
| 100 | |
| 101 | template<typename Ostream, typename T> |
| 102 | using insert_result |
| 103 | = decltype(std::declval<Ostream>() << std::declval<const T&>()); |
| 104 | |
| 105 | template<typename Ostream, typename T> |
| 106 | struct is_insertable<Ostream, T, void_t<insert_result<Ostream, T>>> |
| 107 | : std::true_type |
| 108 | { }; |
| 109 | |
| 110 | // LWG 1203 negative tests |
| 111 | void |
| 112 | test04() |
| 113 | { |
| 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" ); |
| 118 | |
| 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" ); |
| 123 | } |
| 124 | |
| 125 | int |
| 126 | main() |
| 127 | { |
| 128 | test01(); |
| 129 | test02(); |
| 130 | test03(); |
| 131 | test04(); |
| 132 | } |