Debian Codegraph @

libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.class/unique.ptr.observers/dereference.single.pass.cpp

raw
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 // <memory>
10
11 // unique_ptr
12
13 // test op*()
14
15 #include <memory>
16 #include <cassert>
17 #include <vector>
18
19 #include "test_macros.h"
20
21 #if TEST_STD_VER >= 11
22 struct ThrowDereference {
23 TEST_CONSTEXPR_CXX23 ThrowDereference& operator*() noexcept(false);
24 TEST_CONSTEXPR_CXX23 operator bool() const { return false; }
25 };
26
27 struct Deleter {
28 using pointer = ThrowDereference;
29 TEST_CONSTEXPR_CXX23 void operator()(ThrowDereference&) const {}
30 };
31 #endif
32
33 TEST_CONSTEXPR_CXX23 bool test() {
34 ASSERT_NOEXCEPT(*(std::unique_ptr<void>{}));
35 #if TEST_STD_VER >= 11
36 static_assert(noexcept(*std::declval<std::unique_ptr<void>>()), "");
37 #endif
38 {
39 std::unique_ptr<int> p(new int(3));
40 assert(*p == 3);
41 ASSERT_NOEXCEPT(*p);
42 }
43 #if TEST_STD_VER >= 11
44 {
45 std::unique_ptr<std::vector<int>> p(new std::vector<int>{3, 4, 5});
46 assert((*p)[0] == 3);
47 assert((*p)[1] == 4);
48 assert((*p)[2] == 5);
49 ASSERT_NOEXCEPT(*p);
50 }
51 {
52 std::unique_ptr<ThrowDereference> p;
53 ASSERT_NOEXCEPT(*p);
54 }
55 {
56 // The noexcept status of *unique_ptr<>::pointer should be propagated.
57 std::unique_ptr<ThrowDereference, Deleter> p;
58 ASSERT_NOT_NOEXCEPT(*p);
59 }
60 #endif
61
62 return true;
63 }
64
65 int main(int, char**) {
66 test();
67 #if TEST_STD_VER >= 23
68 static_assert(test());
69 #endif
70
71 return 0;
72 }