| 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 reset(element_type* p = nullptr) noexcept; |
| 18 |
|
| 19 |
#include <experimental/memory> |
| 20 |
#include <type_traits> |
| 21 |
#include <cassert> |
| 22 |
|
| 23 |
template <class T, class Object = T> |
| 24 |
constexpr void test_reset() { |
| 25 |
using Ptr = std::experimental::observer_ptr<T>; |
| 26 |
Object obj1, obj2; |
| 27 |
|
| 28 |
{ |
| 29 |
Ptr ptr(&obj1); |
| 30 |
assert(ptr.get() == &obj1); |
| 31 |
ptr.reset(&obj2); |
| 32 |
assert(ptr.get() == &obj2); |
| 33 |
static_assert(noexcept(ptr.reset(&obj2))); |
| 34 |
static_assert(std::is_same<decltype(ptr.reset(&obj2)), void>::value); |
| 35 |
} |
| 36 |
{ |
| 37 |
Ptr ptr(&obj1); |
| 38 |
assert(ptr.get() == &obj1); |
| 39 |
ptr.reset(); |
| 40 |
assert(ptr.get() == nullptr); |
| 41 |
static_assert(noexcept(ptr.reset())); |
| 42 |
static_assert(std::is_same<decltype(ptr.reset()), void>::value); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
struct Bar {}; |
| 47 |
|
| 48 |
constexpr bool test() { |
| 49 |
test_reset<Bar>(); |
| 50 |
test_reset<int>(); |
| 51 |
test_reset<void, int>(); |
| 52 |
|
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
int main(int, char**) { |
| 57 |
test(); |
| 58 |
static_assert(test()); |
| 59 |
|
| 60 |
return 0; |
| 61 |
} |