Defined in header <memory> | ||
---|---|---|
(1) | ||
template< class T > struct owner_less; /* undefined */ | (since C++11) (until C++17) | |
template< class T = void > struct owner_less; /* undefined */ | (since C++17) | |
template< class T > struct owner_less<std::shared_ptr<T>>; | (2) | (since C++11) |
template< class T > struct owner_less<std::weak_ptr<T>>; | (3) | (since C++11) |
template<> struct owner_less<void>; | (4) | (since C++17) |
This function object provides owner-based (as opposed to value-based) mixed-type ordering of both std::weak_ptr
and std::shared_ptr
. The order is such that two smart pointers compare equivalent only if they are both empty or if they both manage the same object, even if the values of the raw pointers obtained by get()
are different (e.g. because they point at different subobjects within the same object).
This class template is the preferred comparison predicate when building associative containers with std::shared_ptr
or std::weak_ptr
as keys, that is,
std::map<std::shared_ptr<T>, U, std::owner_less<std::shared_ptr<T>>>
or.
std::map<std::weak_ptr<T>, U, std::owner_less<std::weak_ptr<T>>>
.
The default operator<
is not defined for weak pointers, and may wrongly consider two shared pointers for the same object non-equivalent (see shared_ptr::owner_before
).
The standard library provides a specialization of
| (since C++17) |
Member types
| (until C++20) |
operator() | compares its arguments using owner-based semantics (function) |
bool operator()( const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs ) const; | (since C++11) (member only of owner_less<shared_ptr<T>> template specialization) | |
bool operator()( const std::shared_ptr<T>& lhs, const std::weak_ptr<T>& rhs ) const; | (since C++11) | |
bool operator()( const std::weak_ptr<T>& lhs, const std::shared_ptr<T>& rhs ) const; | (since C++11) | |
bool operator()( const std::weak_ptr<T>& lhs, const std::weak_ptr<T>& rhs ) const; | (since C++11) (member only of owner_less<weak_ptr<T>> template specialization) |
Compares lhs
and rhs
using owner-based semantics. Effectively calls lhs.owner_before(rhs)
.
The ordering is strict weak ordering relation.
lhs
and rhs
are equivalent only if they are both empty or share ownership.
lhs, rhs | - | shared-ownership pointers to compare |
true
if lhs
is less than rhs
as determined by the owner-based ordering.
(none) | (until C++17) |
noexcept specification: noexcept | (since C++17) |
provides owner-based ordering of shared pointers (public member function of std::shared_ptr ) |
|
provides owner-based ordering of weak pointers (public member function of std::weak_ptr ) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/memory/owner_less