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