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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: no-localization
10 // UNSUPPORTED: c++03, c++11, c++14, c++17
11 
12 // concept checking istream_view
13 
14 #include <istream>
15 #include <ranges>
16 
17 #include "test_macros.h"
18 
19 template <class Val, class CharT, class Traits = std::char_traits<CharT>>
20 concept HasIstreamView = requires { typename std::ranges::basic_istream_view<Val, CharT, Traits>; };
21 
22 static_assert(HasIstreamView<int, char>);
23 
24 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
25 static_assert(HasIstreamView<int, wchar_t>);
26 #endif
27 
28 // Unmovable Val
29 struct Unmovable {
30   Unmovable()            = default;
31   Unmovable(Unmovable&&) = delete;
32   template <class CharT>
operator >>(std::basic_istream<CharT> & x,const Unmovable &)33   friend std::basic_istream<CharT>& operator>>(std::basic_istream<CharT>& x, const Unmovable&) {
34     return x;
35   }
36 };
37 static_assert(!HasIstreamView<Unmovable, char>);
38 
39 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
40 static_assert(!HasIstreamView<Unmovable, wchar_t>);
41 #endif
42 
43 // !default_initializable<Val>
44 struct NoDefaultCtor {
NoDefaultCtorNoDefaultCtor45   NoDefaultCtor(int) {}
operator >>(std::istream & x,const NoDefaultCtor &)46   friend std::istream& operator>>(std::istream& x, const NoDefaultCtor&) { return x; }
47 };
48 static_assert(!HasIstreamView<NoDefaultCtor, char>);
49 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
50 static_assert(!HasIstreamView<NoDefaultCtor, wchar_t>);
51 #endif
52 
53 // !stream-extractable<Val, CharT, Traits>
54 struct Foo {};
55 static_assert(!HasIstreamView<Foo, char>);
56 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
57 static_assert(!HasIstreamView<Foo, wchar_t>);
58 #endif
59 
60 template <class T>
61 concept OnlyInputRange = std::ranges::input_range<T> && !std::ranges::forward_range<T>;
62 
63 static_assert(OnlyInputRange<std::ranges::istream_view<int>>);
64 static_assert(OnlyInputRange<std::ranges::istream_view<long>>);
65 static_assert(OnlyInputRange<std::ranges::istream_view<double>>);
66 static_assert(OnlyInputRange<std::ranges::istream_view<char>>);
67 
68 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
69 static_assert(OnlyInputRange<std::ranges::wistream_view<int>>);
70 static_assert(OnlyInputRange<std::ranges::wistream_view<long>>);
71 static_assert(OnlyInputRange<std::ranges::wistream_view<double>>);
72 static_assert(OnlyInputRange<std::ranges::wistream_view<wchar_t>>);
73 #endif
74