1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <memory>
11
12 // template <class T> struct owner_less;
13 //
14 // template <class T>
15 // struct owner_less<shared_ptr<T> >
16 // : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
17 // {
18 // typedef bool result_type;
19 // bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const;
20 // bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
21 // bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
22 // };
23 //
24 // template <class T>
25 // struct owner_less<weak_ptr<T> >
26 // : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
27 // {
28 // typedef bool result_type;
29 // bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const;
30 // bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
31 // bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
32 // };
33
34 #include <memory>
35 #include <cassert>
36
main()37 int main()
38 {
39 const std::shared_ptr<int> p1(new int);
40 const std::shared_ptr<int> p2 = p1;
41 const std::shared_ptr<int> p3(new int);
42 const std::weak_ptr<int> w1(p1);
43 const std::weak_ptr<int> w2(p2);
44 const std::weak_ptr<int> w3(p3);
45
46 {
47 typedef std::owner_less<std::shared_ptr<int> > CS;
48 CS cs;
49
50 static_assert((std::is_same<std::shared_ptr<int>, CS::first_argument_type>::value), "" );
51 static_assert((std::is_same<std::shared_ptr<int>, CS::second_argument_type>::value), "" );
52 static_assert((std::is_same<bool, CS::result_type>::value), "" );
53
54 assert(!cs(p1, p2));
55 assert(!cs(p2, p1));
56 assert(cs(p1 ,p3) || cs(p3, p1));
57 assert(cs(p3, p1) == cs(p3, p2));
58
59 assert(!cs(p1, w2));
60 assert(!cs(p2, w1));
61 assert(cs(p1, w3) || cs(p3, w1));
62 assert(cs(p3, w1) == cs(p3, w2));
63 }
64 {
65 typedef std::owner_less<std::weak_ptr<int> > CS;
66 CS cs;
67
68 static_assert((std::is_same<std::weak_ptr<int>, CS::first_argument_type>::value), "" );
69 static_assert((std::is_same<std::weak_ptr<int>, CS::second_argument_type>::value), "" );
70 static_assert((std::is_same<bool, CS::result_type>::value), "" );
71
72 assert(!cs(w1, w2));
73 assert(!cs(w2, w1));
74 assert(cs(w1, w3) || cs(w3, w1));
75 assert(cs(w3, w1) == cs(w3, w2));
76
77 assert(!cs(w1, p2));
78 assert(!cs(w2, p1));
79 assert(cs(w1, p3) || cs(w3, p1));
80 assert(cs(w3, p1) == cs(w3, p2));
81 }
82 }
83