std::owner_less (3) - Linux Manuals
std::owner_less: std::owner_less
NAME
std::owner_less - std::owner_less
Synopsis
Defined in header <memory>
template< class T > (since C++11)
struct owner_less; /* undefined */ (until C++17)
template< class T = void > (since C++17)
struct owner_less; /* undefined */
template< class T > (1) (2) (since C++11)
struct owner_less<std::shared_ptr<T>>;
template< class T > (3) (since C++11)
struct owner_less<std::weak_ptr<T>>;
template<> (4) (since C++17)
struct owner_less<void>;
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 share ownership, 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)
Specializations
The standard library provides a specialization of std::owner_less when T is not specified. In this case, the parameter types are deduced from the arguments (each of which must still be either a std::shared_ptr or a std::weak_ptr).
owner_less<void> (class template specialization)
Member types
Member type Definition
result_type(deprecated in c++17) 2-3) bool (until C++20)
first_argument_type(deprecated in c++17) 2) std::shared_ptr<T>
second_argument_type(deprecated in c++17) 2) std::shared_ptr<T>
Member functions
operator() (function)
std::owner_less::operator()
bool operator()( const std::shared_ptr<T>& lhs, (since C++11)
const std::shared_ptr<T>& rhs ) const; (member only of owner_less<shared_ptr<T>> template specialization)
bool operator()( const std::shared_ptr<T>& lhs, (since C++11)
const std::weak_ptr<T>& rhs ) const;
bool operator()( const std::weak_ptr<T>& lhs, (since C++11)
const std::shared_ptr<T>& rhs ) const;
bool operator()( const std::weak_ptr<T>& lhs, (since C++11)
const std::weak_ptr<T>& rhs ) const; (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.
Parameters
lhs, rhs - shared-ownership pointers to compare
Return value
true if lhs is less than rhs as determined by the owner-based ordering.
Exceptions
(none) (until C++17)
noexcept specification: (since C++17)
noexcept
See also
owner_before (public member function of std::shared_ptr<T>)
owner_before (public member function of std::weak_ptr<T>)