Debian Codegraph @

libstdc++-v3/testsuite/20_util/shared_ptr/observers/get.cc

raw
1 // { dg-do run { target c++11 } }
2 // { dg-require-effective-target hosted }
3
4 // Copyright (C) 2005-2025 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
20
21 // 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
22
23 #include <memory>
24 #include <testsuite_hooks.h>
25
26 struct A
27 {
28 A() : i() {}
29 int i;
30 };
31
32 // 20.6.6.2.5 shared_ptr observers [util.smartptr.shared.obs]
33
34 // get
35 void
36 test01()
37 {
38 A * const a = new A;
39 const std::shared_ptr<A> p(a);
40 VERIFY( p.get() == a );
41 static_assert( noexcept(p.get()), "non-throwing" );
42 }
43
44 // operator*
45 void
46 test02()
47 {
48 A * const a = new A;
49 const std::shared_ptr<A> p(a);
50 VERIFY( &*p == a );
51 static_assert( noexcept(*p), "non-throwing" );
52 }
53
54 // operator->
55 void
56 test03()
57 {
58 A * const a = new A;
59 const std::shared_ptr<A> p(a);
60 VERIFY( &p->i == &a->i );
61 static_assert( noexcept(p->i), "non-throwing" );
62 }
63
64 void
65 test04()
66 {
67 #if !(defined _GLIBCXX_DEBUG && defined _GLIBCXX_DEBUG_PEDANTIC)
68 std::shared_ptr<int> p;
69 auto np = p.operator->();
70 VERIFY( np == nullptr );
71 #endif
72 }
73
74 int
75 main()
76 {
77 test01();
78 test02();
79 test03();
80 test04();
81 return 0;
82 }