//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // template struct owner_less; // // template // struct owner_less > // : binary_function, shared_ptr, bool> // { // typedef bool result_type; // bool operator()(shared_ptr const&, shared_ptr const&) const noexcept; // bool operator()(shared_ptr const&, weak_ptr const&) const noexcept; // bool operator()(weak_ptr const&, shared_ptr const&) const noexcept; // }; // // template // struct owner_less > // : binary_function, weak_ptr, bool> // { // typedef bool result_type; // bool operator()(weak_ptr const&, weak_ptr const&) const noexcept; // bool operator()(shared_ptr const&, weak_ptr const&) const noexcept; // bool operator()(weak_ptr const&, shared_ptr const&) const noexcept; // }; // // Added in C++17 // template<> struct owner_less // { // template // bool operator()(shared_ptr const&, shared_ptr const&) const noexcept; // template // bool operator()(shared_ptr const&, weak_ptr const&) const noexcept; // template // bool operator()(weak_ptr const&, shared_ptr const&) const noexcept; // template // bool operator()(weak_ptr const&, weak_ptr const&) const noexcept; // // typedef unspecified is_transparent; // }; #include #include #include #include "test_macros.h" struct X {}; int main(int, char**) { const std::shared_ptr p1(new int); const std::shared_ptr p2 = p1; const std::shared_ptr p3(new int); const std::weak_ptr w1(p1); const std::weak_ptr w2(p2); const std::weak_ptr w3(p3); { typedef std::owner_less > CS; CS cs; static_assert((std::is_same, CS::first_argument_type>::value), "" ); static_assert((std::is_same, CS::second_argument_type>::value), "" ); static_assert((std::is_same::value), "" ); assert(!cs(p1, p2)); assert(!cs(p2, p1)); assert(cs(p1 ,p3) || cs(p3, p1)); assert(cs(p3, p1) == cs(p3, p2)); ASSERT_NOEXCEPT(cs(p1, p1)); assert(!cs(p1, w2)); assert(!cs(p2, w1)); assert(cs(p1, w3) || cs(p3, w1)); assert(cs(p3, w1) == cs(p3, w2)); ASSERT_NOEXCEPT(cs(p1, w1)); ASSERT_NOEXCEPT(cs(w1, p1)); } { typedef std::owner_less > CS; CS cs; static_assert((std::is_same, CS::first_argument_type>::value), "" ); static_assert((std::is_same, CS::second_argument_type>::value), "" ); static_assert((std::is_same::value), "" ); assert(!cs(w1, w2)); assert(!cs(w2, w1)); assert(cs(w1, w3) || cs(w3, w1)); assert(cs(w3, w1) == cs(w3, w2)); ASSERT_NOEXCEPT(cs(w1, w1)); assert(!cs(w1, p2)); assert(!cs(w2, p1)); assert(cs(w1, p3) || cs(w3, p1)); assert(cs(w3, p1) == cs(w3, p2)); ASSERT_NOEXCEPT(cs(w1, p1)); ASSERT_NOEXCEPT(cs(p1, w1)); } #if TEST_STD_VER > 14 { std::shared_ptr sp1; std::shared_ptr sp2; std::shared_ptr sp3; std::weak_ptr wp1; std::owner_less<> cmp; assert(!cmp(sp1, sp2)); assert(!cmp(sp1, wp1)); assert(!cmp(sp1, sp3)); assert(!cmp(wp1, sp1)); assert(!cmp(wp1, wp1)); ASSERT_NOEXCEPT(cmp(sp1, sp1)); ASSERT_NOEXCEPT(cmp(sp1, wp1)); ASSERT_NOEXCEPT(cmp(wp1, sp1)); ASSERT_NOEXCEPT(cmp(wp1, wp1)); } { // test heterogeneous lookups std::set, std::owner_less<>> s; std::shared_ptr vp; assert(s.find(vp) == s.end()); } #endif return 0; }