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// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
10
11// <memory>
12
13// template <class T> struct owner_less;
14//
15// template <class T>
16// struct owner_less<shared_ptr<T> >
17// : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
18// {
19// typedef bool result_type;
20// bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
21// bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
22// bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
23// };
24//
25// template <class T>
26// struct owner_less<weak_ptr<T> >
27// : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
28// {
29// typedef bool result_type;
30// bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
31// bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
32// bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
33// };
34//
35// Added in C++17
36// template<> struct owner_less<void>
37// {
38// template<class T, class U>
39// bool operator()(shared_ptr<T> const&, shared_ptr<U> const&) const noexcept;
40// template<class T, class U>
41// bool operator()(shared_ptr<T> const&, weak_ptr<U> const&) const noexcept;
42// template<class T, class U>
43// bool operator()(weak_ptr<T> const&, shared_ptr<U> const&) const noexcept;
44// template<class T, class U>
45// bool operator()(weak_ptr<T> const&, weak_ptr<U> const&) const noexcept;
46//
47// typedef unspecified is_transparent;
48// };
49
50#include <memory>
51#include <cassert>
52#include <set>
53#include "test_macros.h"
54
55struct X {};
56
57int main(int, char**)
58{
59 const std::shared_ptr<int> p1(new int);
60 const std::shared_ptr<int> p2 = p1;
61 const std::shared_ptr<int> p3(new int);
62 const std::weak_ptr<int> w1(p1);
63 const std::weak_ptr<int> w2(p2);
64 const std::weak_ptr<int> w3(p3);
65
66 {
67 typedef std::owner_less<std::shared_ptr<int> > CS;
68 CS cs;
69
70#if TEST_STD_VER <= 17
71 static_assert((std::is_same<std::shared_ptr<int>, CS::first_argument_type>::value), "" );
72 static_assert((std::is_same<std::shared_ptr<int>, CS::second_argument_type>::value), "" );
73 static_assert((std::is_same<bool, CS::result_type>::value), "" );
74#endif
75
76 assert(!cs(p1, p2));
77 assert(!cs(p2, p1));
78 assert(cs(p1 ,p3) || cs(p3, p1));
79 assert(cs(p3, p1) == cs(p3, p2));
80 ASSERT_NOEXCEPT(cs(p1, p1));
81
82 assert(!cs(p1, w2));
83 assert(!cs(p2, w1));
84 assert(cs(p1, w3) || cs(p3, w1));
85 assert(cs(p3, w1) == cs(p3, w2));
86 ASSERT_NOEXCEPT(cs(p1, w1));
87 ASSERT_NOEXCEPT(cs(w1, p1));
88 }
89 {
90 typedef std::owner_less<std::weak_ptr<int> > CS;
91 CS cs;
92
93#if TEST_STD_VER <= 17
94 static_assert((std::is_same<std::weak_ptr<int>, CS::first_argument_type>::value), "" );
95 static_assert((std::is_same<std::weak_ptr<int>, CS::second_argument_type>::value), "" );
96 static_assert((std::is_same<bool, CS::result_type>::value), "" );
97#endif
98
99 assert(!cs(w1, w2));
100 assert(!cs(w2, w1));
101 assert(cs(w1, w3) || cs(w3, w1));
102 assert(cs(w3, w1) == cs(w3, w2));
103 ASSERT_NOEXCEPT(cs(w1, w1));
104
105 assert(!cs(w1, p2));
106 assert(!cs(w2, p1));
107 assert(cs(w1, p3) || cs(w3, p1));
108 assert(cs(w3, p1) == cs(w3, p2));
109 ASSERT_NOEXCEPT(cs(w1, p1));
110 ASSERT_NOEXCEPT(cs(p1, w1));
111 }
112#if TEST_STD_VER > 14
113 {
114 std::shared_ptr<int> sp1;
115 std::shared_ptr<void> sp2;
116 std::shared_ptr<long> sp3;
117 std::weak_ptr<int> wp1;
118
119 std::owner_less<> cmp;
120 assert(!cmp(sp1, sp2));
121 assert(!cmp(sp1, wp1));
122 assert(!cmp(sp1, sp3));
123 assert(!cmp(wp1, sp1));
124 assert(!cmp(wp1, wp1));
125 ASSERT_NOEXCEPT(cmp(sp1, sp1));
126 ASSERT_NOEXCEPT(cmp(sp1, wp1));
127 ASSERT_NOEXCEPT(cmp(wp1, sp1));
128 ASSERT_NOEXCEPT(cmp(wp1, wp1));
129 }
130 {
131 // test heterogeneous lookups
132 std::set<std::shared_ptr<X>, std::owner_less<>> s;
133 std::shared_ptr<void> vp;
134 assert(s.find(vp) == s.end());
135 }
136#endif
137
138 return 0;
139}
140

source code of libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.ownerless/owner_less.pass.cpp