|
|
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 bothstd::weak_ptr andstd::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 byget()
are different (e.g. because they point at different subobjects within the same object).
The defaultoperator< is not defined for weak pointers, and may wrongly consider two shared pointers for the same object non-equivalent (seestd::shared_ptr::owner_before).
SpecializationsThe standard library provides a specialization of
| (since C++17) |
Nested types
| (until C++20) |
operator() | compares its arguments using owner-based semantics (function) |
member only of specialization(2) | ||
bool operator()(conststd::shared_ptr<T>& lhs, conststd::shared_ptr<T>& rhs)constnoexcept; | (since C++11) | |
member only of specialization(3) | ||
bool operator()(conststd::weak_ptr<T>& lhs, conststd::weak_ptr<T>& rhs)constnoexcept; | (since C++11) | |
member of both template specializations | ||
bool operator()(conststd::shared_ptr<T>& lhs, conststd::weak_ptr<T>& rhs)constnoexcept; | (since C++11) | |
bool operator()(conststd::weak_ptr<T>& lhs, conststd::shared_ptr<T>& rhs)constnoexcept; | (since C++11) | |
Compareslhs andrhs using owner-based semantics. Effectively callslhs.owner_before(rhs).
The ordering is strict weak ordering relation.
lhs andrhs are equivalent only if they are both empty or share ownership.
lhs, rhs | - | shared-ownership pointers to compare |
true iflhs isless thanrhs as determined by the owner-based ordering,false otherwise.
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2873 | C++11 | operator() was not required to be noexcept | required to be noexcept |
provides owner-based ordering of shared pointers (public member function of std::shared_ptr<T> )[edit] | |
provides owner-based ordering of weak pointers (public member function of std::weak_ptr<T> )[edit] |