1 | // -*- C++ -*- |
2 | //===----------------------------------------------------------------------===// |
3 | // |
4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
5 | // See https://llvm.org/LICENSE.txt for license information. |
6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
7 | // |
8 | //===----------------------------------------------------------------------===// |
9 | |
10 | // UNSUPPORTED: c++03, c++11, c++14 |
11 | // REQUIRES: c++experimental |
12 | |
13 | // <experimental/memory> |
14 | |
15 | // observer_ptr |
16 | // |
17 | // constexpr std::add_lvalue_reference_t<element_type> operator*() const; |
18 | // constexpr element_type* operator->() const noexcept; |
19 | |
20 | #include <experimental/memory> |
21 | #include <type_traits> |
22 | #include <cassert> |
23 | |
24 | template <class T, class Object = T> |
25 | constexpr void test_deref() { |
26 | using Ptr = std::experimental::observer_ptr<T>; |
27 | Object obj; |
28 | |
29 | { |
30 | Ptr const ptr(&obj); |
31 | T& r = *ptr; |
32 | assert(&r == &obj); |
33 | } |
34 | { |
35 | Ptr const ptr(&obj); |
36 | T* r = ptr.operator->(); |
37 | assert(r == &obj); |
38 | static_assert(noexcept(ptr.operator->())); |
39 | } |
40 | } |
41 | |
42 | struct Bar {}; |
43 | struct Foo { |
44 | int member = 42; |
45 | }; |
46 | |
47 | constexpr bool test() { |
48 | test_deref<Bar>(); |
49 | test_deref<int>(); |
50 | |
51 | { |
52 | Foo foo; |
53 | std::experimental::observer_ptr<Foo> ptr(&foo); |
54 | assert(&ptr->member == &foo.member); |
55 | } |
56 | |
57 | return true; |
58 | } |
59 | |
60 | int main(int, char**) { |
61 | test(); |
62 | static_assert(test()); |
63 | |
64 | return 0; |
65 | } |
66 | |