blob: d091ae99fc272af6f661743256a70b65234f1e6f [file] [log] [blame]
Howard Hinnantc52f43e2010-08-22 00:59:46 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc52f43e2010-08-22 00:59:46 +00007//
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
37int 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
Marshall Clowb1529832015-01-07 20:54:51 +000050 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
Howard Hinnantc52f43e2010-08-22 00:59:46 +000054 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
Marshall Clowb1529832015-01-07 20:54:51 +000068 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
Howard Hinnantc52f43e2010-08-22 00:59:46 +000072 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}