owner_less.pass.cpp revision 3f159e874c5c4d86f244d5950fe1114d8d7f6346
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// Added in C++17 35// template<> struct owner_less<void> 36// { 37// template<class T, class U> 38// bool operator()(shared_ptr<T> const&, shared_ptr<U> const&) const; 39// template<class T, class U> 40// bool operator()(shared_ptr<T> const&, weak_ptr<U> const&) const; 41// template<class T, class U> 42// bool operator()(weak_ptr<T> const&, shared_ptr<U> const&) const; 43// template<class T, class U> 44// bool operator()(weak_ptr<T> const&, weak_ptr<U> const&) const; 45// 46// typedef unspecified is_transparent; 47// }; 48 49#include <memory> 50#include <cassert> 51#include <set> 52#include "test_macros.h" 53 54struct X {}; 55 56int main() 57{ 58 const std::shared_ptr<int> p1(new int); 59 const std::shared_ptr<int> p2 = p1; 60 const std::shared_ptr<int> p3(new int); 61 const std::weak_ptr<int> w1(p1); 62 const std::weak_ptr<int> w2(p2); 63 const std::weak_ptr<int> w3(p3); 64 65 { 66 typedef std::owner_less<std::shared_ptr<int> > CS; 67 CS cs; 68 69 static_assert((std::is_same<std::shared_ptr<int>, CS::first_argument_type>::value), "" ); 70 static_assert((std::is_same<std::shared_ptr<int>, CS::second_argument_type>::value), "" ); 71 static_assert((std::is_same<bool, CS::result_type>::value), "" ); 72 73 assert(!cs(p1, p2)); 74 assert(!cs(p2, p1)); 75 assert(cs(p1 ,p3) || cs(p3, p1)); 76 assert(cs(p3, p1) == cs(p3, p2)); 77 78 assert(!cs(p1, w2)); 79 assert(!cs(p2, w1)); 80 assert(cs(p1, w3) || cs(p3, w1)); 81 assert(cs(p3, w1) == cs(p3, w2)); 82 } 83 { 84 typedef std::owner_less<std::weak_ptr<int> > CS; 85 CS cs; 86 87 static_assert((std::is_same<std::weak_ptr<int>, CS::first_argument_type>::value), "" ); 88 static_assert((std::is_same<std::weak_ptr<int>, CS::second_argument_type>::value), "" ); 89 static_assert((std::is_same<bool, CS::result_type>::value), "" ); 90 91 assert(!cs(w1, w2)); 92 assert(!cs(w2, w1)); 93 assert(cs(w1, w3) || cs(w3, w1)); 94 assert(cs(w3, w1) == cs(w3, w2)); 95 96 assert(!cs(w1, p2)); 97 assert(!cs(w2, p1)); 98 assert(cs(w1, p3) || cs(w3, p1)); 99 assert(cs(w3, p1) == cs(w3, p2)); 100 } 101#if TEST_STD_VER > 14 102 { 103 std::shared_ptr<int> sp1; 104 std::shared_ptr<void> sp2; 105 std::shared_ptr<long> sp3; 106 std::weak_ptr<int> wp1; 107 108 std::owner_less<> cmp; 109 cmp(sp1, sp2); 110 cmp(sp1, wp1); 111 cmp(sp1, sp3); 112 cmp(wp1, sp1); 113 cmp(wp1, wp1); 114 } 115 { 116 // test heterogeneous lookups 117 std::set<std::shared_ptr<X>, std::owner_less<>> s; 118 std::shared_ptr<void> vp; 119 s.find(vp); 120 } 121#endif 122} 123