| 1 |
// { dg-do run { target c++11 } } |
| 2 |
|
| 3 |
// Copyright (C) 2016-2025 Free Software Foundation, Inc. |
| 4 |
// |
| 5 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 6 |
// software; you can redistribute it and/or modify it under the |
| 7 |
// terms of the GNU General Public License as published by the |
| 8 |
// Free Software Foundation; either version 3, or (at your option) |
| 9 |
// any later version. |
| 10 |
|
| 11 |
// This library is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
|
| 16 |
// You should have received a copy of the GNU General Public License along |
| 17 |
// with this library; see the file COPYING3. If not see |
| 18 |
// <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
// C++11 27.7.2.6 Rvalue stream extraction [istream.rvalue] |
| 21 |
|
| 22 |
#include <sstream> |
| 23 |
|
| 24 |
template<typename Istream, typename T, typename = void> |
| 25 |
struct is_extractable : std::false_type |
| 26 |
{ }; |
| 27 |
|
| 28 |
template<typename> using void_t = void; |
| 29 |
|
| 30 |
template<typename Istream, typename T> |
| 31 |
using extract_result |
| 32 |
= decltype(std::declval<Istream>() >> std::declval<const T&>()); |
| 33 |
|
| 34 |
template<typename Istream, typename T> |
| 35 |
struct is_extractable<Istream, T, void_t<extract_result<Istream, T>>> |
| 36 |
: std::true_type |
| 37 |
{ }; |
| 38 |
|
| 39 |
struct X {}; |
| 40 |
std::istream& operator>>(std::istream&, X&) = delete; |
| 41 |
|
| 42 |
struct Y {}; |
| 43 |
std::istream& operator>>(std::istream& is, Y&) {return is;} |
| 44 |
std::istream& operator>>(std::istream& is, Y&&) {return is;} |
| 45 |
|
| 46 |
struct Z{}; |
| 47 |
|
| 48 |
void test01() |
| 49 |
{ |
| 50 |
Y y; |
| 51 |
std::istringstream is; |
| 52 |
is >> y; |
| 53 |
is >> Y(); |
| 54 |
std::istringstream() >> y; |
| 55 |
std::istringstream() >> Y(); |
| 56 |
static_assert(!is_extractable<std::istream&, X&>::value, ""); |
| 57 |
static_assert(!is_extractable<std::istream&&, X&>::value, ""); |
| 58 |
static_assert(!is_extractable<std::istream&, X&&>::value, ""); |
| 59 |
static_assert(!is_extractable<std::istream&&, X&&>::value, ""); |
| 60 |
static_assert(is_extractable<std::istream&, Y&>::value, ""); |
| 61 |
static_assert(is_extractable<std::istream&&, Y&>::value, ""); |
| 62 |
static_assert(is_extractable<std::istream&, Y&&>::value, ""); |
| 63 |
static_assert(is_extractable<std::istream&&, Y&&>::value, ""); |
| 64 |
static_assert(!is_extractable<std::istream&, Z&>::value, ""); |
| 65 |
static_assert(!is_extractable<std::istream&&, Z&>::value, ""); |
| 66 |
static_assert(!is_extractable<std::istream&, Z&&>::value, ""); |
| 67 |
static_assert(!is_extractable<std::istream&&, Z&&>::value, ""); |
| 68 |
} |
| 69 |
|
| 70 |
int main() |
| 71 |
{ |
| 72 |
test01(); |
| 73 |
} |