| 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 void swap(observer_ptr& other) noexcept; |
| 18 | // |
| 19 | // template <class W> |
| 20 | // void swap(observer_ptr<W>& lhs, observer_ptr<W>& rhs) noexcept; |
| 21 | |
| 22 | #include <experimental/memory> |
| 23 | #include <type_traits> |
| 24 | #include <cassert> |
| 25 | |
| 26 | template <class T, class Object = T> |
| 27 | constexpr void test_swap() { |
| 28 | using Ptr = std::experimental::observer_ptr<T>; |
| 29 | Object obj1, obj2; |
| 30 | |
| 31 | { |
| 32 | Ptr ptr1(&obj1); |
| 33 | Ptr ptr2(&obj2); |
| 34 | |
| 35 | assert(ptr1.get() == &obj1); |
| 36 | assert(ptr2.get() == &obj2); |
| 37 | |
| 38 | ptr1.swap(ptr2); |
| 39 | |
| 40 | assert(ptr1.get() == &obj2); |
| 41 | assert(ptr2.get() == &obj1); |
| 42 | |
| 43 | static_assert(noexcept(ptr1.swap(ptr2))); |
| 44 | static_assert(std::is_same<decltype(ptr1.swap(ptr2)), void>::value); |
| 45 | } |
| 46 | |
| 47 | { |
| 48 | Ptr ptr1(&obj1); |
| 49 | Ptr ptr2(&obj2); |
| 50 | |
| 51 | assert(ptr1.get() == &obj1); |
| 52 | assert(ptr2.get() == &obj2); |
| 53 | |
| 54 | std::experimental::swap(ptr1, ptr2); |
| 55 | |
| 56 | assert(ptr1.get() == &obj2); |
| 57 | assert(ptr2.get() == &obj1); |
| 58 | |
| 59 | static_assert(noexcept(std::experimental::swap(ptr1, ptr2))); |
| 60 | static_assert(std::is_same<decltype(std::experimental::swap(ptr1, ptr2)), void>::value); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | struct Bar {}; |
| 65 | |
| 66 | constexpr bool test() { |
| 67 | test_swap<Bar>(); |
| 68 | test_swap<int>(); |
| 69 | test_swap<void, int>(); |
| 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | int main(int, char**) { |
| 75 | test(); |
| 76 | static_assert(test()); |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |