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 get_deleter()
14
15#include <memory>
16#include <cassert>
17#include <type_traits>
18
19#include "test_macros.h"
20
21struct Deleter {
22 TEST_CONSTEXPR_CXX23 Deleter() {}
23
24 TEST_CONSTEXPR_CXX23 void operator()(void*) const {}
25
26 TEST_CONSTEXPR_CXX23 int test() { return 5; }
27 TEST_CONSTEXPR_CXX23 int test() const { return 6; }
28};
29
30template <bool IsArray>
31TEST_CONSTEXPR_CXX23 void test_basic() {
32 typedef typename std::conditional<IsArray, int[], int>::type VT;
33 {
34 std::unique_ptr<int, Deleter> p;
35 assert(p.get_deleter().test() == 5);
36 }
37 {
38 const std::unique_ptr<VT, Deleter> p;
39 assert(p.get_deleter().test() == 6);
40 }
41 {
42 typedef std::unique_ptr<VT, const Deleter&> UPtr;
43 const Deleter d;
44 UPtr p(nullptr, d);
45 const UPtr& cp = p;
46 ASSERT_SAME_TYPE(decltype(p.get_deleter()), const Deleter&);
47 ASSERT_SAME_TYPE(decltype(cp.get_deleter()), const Deleter&);
48 assert(p.get_deleter().test() == 6);
49 assert(cp.get_deleter().test() == 6);
50 }
51 {
52 typedef std::unique_ptr<VT, Deleter&> UPtr;
53 Deleter d;
54 UPtr p(nullptr, d);
55 const UPtr& cp = p;
56 ASSERT_SAME_TYPE(decltype(p.get_deleter()), Deleter&);
57 ASSERT_SAME_TYPE(decltype(cp.get_deleter()), Deleter&);
58 assert(p.get_deleter().test() == 5);
59 assert(cp.get_deleter().test() == 5);
60 }
61}
62
63TEST_CONSTEXPR_CXX23 bool test() {
64 test_basic</*IsArray*/ false>();
65 test_basic<true>();
66
67 return true;
68}
69
70int main(int, char**) {
71 test();
72#if TEST_STD_VER >= 23
73 static_assert(test());
74#endif
75
76 return 0;
77}
78

source code of libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.class/unique.ptr.observers/get_deleter.pass.cpp