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

source code of libcxx/test/std/experimental/memory/memory.observer.ptr/release.pass.cpp