Debian Codegraph @

libstdc++-v3/testsuite/20_util/unique_ptr/io/lwg2948.cc

raw
1 // Copyright (C) 2020-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++20 } }
19
20 #include <memory>
21 #include <sstream>
22 #include <testsuite_hooks.h>
23
24 template<typename S, typename T>
25 concept streamable = requires (S& o, const T& p) { o << p; };
26
27 template<typename T, typename D>
28 bool
29 check(const std::unique_ptr<T, D>& p)
30 {
31 std::ostringstream ss1, ss2;
32 ss1 << p;
33 ss2 << p.get();
34 return ss1.str() == ss2.str();
35 }
36
37 void
38 test01()
39 {
40 static_assert( streamable<std::ostream, std::unique_ptr<int>> );
41
42 std::unique_ptr<int> p;
43 VERIFY( check(p) );
44 p = std::make_unique<int>();
45 VERIFY( check(p) );
46 }
47
48 template<typename>
49 struct deleter
50 {
51 struct pointer
52 {
53 pointer() { }
54 pointer(std::nullptr_t) { }
55 explicit operator bool() const { return false; }
56 bool operator==(pointer) const { return true; }
57 };
58
59 void operator()(pointer) const { }
60 };
61
62 template<typename C, typename Traits>
63 int
64 operator<<(std::basic_ostream<C, Traits>& o, typename deleter<C>::pointer&&)
65 {
66 o << C{'P'};
67 return 1; // no requirement that this operator returns the stream
68 }
69
70 template<typename D>
71 using Unique_ptr = std::unique_ptr<typename D::pointer, D>;
72
73 static_assert( streamable<std::ostream, Unique_ptr<deleter<char>>> );
74 static_assert( ! streamable<std::ostream, Unique_ptr<deleter<wchar_t>>> );
75 #ifdef _GLIBCXX_USE_WCHAR_T
76 static_assert( ! streamable<std::wostream, Unique_ptr<deleter<char>>> );
77 static_assert( streamable<std::wostream, Unique_ptr<deleter<wchar_t>>> );
78 #endif
79
80 void
81 test02()
82 {
83 Unique_ptr<deleter<char>> p;
84 VERIFY( check(p) );
85 }
86
87 int
88 main()
89 {
90 test01();
91 test02();
92 }