std::owner_less
Defined in header <memory>
.
- C++11
- C++17
// 1)
template< class T >
struct owner_less; /* undefined */
// 2)
template< class T >
struct owner_less<std::shared_ptr<T>>;
// 3)
template< class T >
struct owner_less<std::weak_ptr<T>>;
// 1)
template< class T = void >
struct owner_less; /* undefined */
// 2)
template< class T >
struct owner_less<std::shared_ptr<T>>;
// 3)
template< class T >
struct owner_less<std::weak_ptr<T>>;
// 4)
template<>
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).