Namespaces
Variants
Views
Actions

std::owner_less

From cppreference.com
< cpp‎ | memory
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

Elementary string conversions
(C++17)
(C++17)
 
Dynamic memory management
Smart pointers
(C++11)
(C++11)
(C++11)
(until C++17)
owner_less
(C++11)
Allocators
Memory resources
Uninitialized storage
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Garbage collection support
Miscellaneous
(C++20)
(C++11)
(C++11)
C Library
Low level memory management
 
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 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)

Contents

[edit] 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).

function object providing mixed-type owner-based ordering of shared and weak pointers, regardless of the type of the pointee
(class template specialization) [edit]
(since C++17)

Member types

Member type Definition
result_type(deprecated in c++17) 2-3) bool
first_argument_type(deprecated in c++17) 2) std::shared_ptr<T>
3) std::weak_ptr<T>
second_argument_type(deprecated in c++17) 2) std::shared_ptr<T>
3) std::weak_ptr<T>
(until C++20)

[edit] Member functions

operator()
compares its arguments using owner-based semantics
(function)

std::owner_less::operator()

member only of owner_less<shared_ptr<T>> template specialization
bool operator()( const std::shared_ptr<T>& lhs,
                 const std::shared_ptr<T>& rhs ) const noexcept;
(since C++11)
member only of owner_less<weak_ptr<T>> template specialization
bool operator()( const std::weak_ptr<T>& lhs,
                 const std::weak_ptr<T>& rhs ) const noexcept;
(since C++11)
member of both template specializations
bool operator()( const std::shared_ptr<T>& lhs,
                 const std::weak_ptr<T>& rhs ) const noexcept;
(since C++11)
bool operator()( const std::weak_ptr<T>& lhs,
                 const std::shared_ptr<T>& rhs ) const noexcept;
(since C++11)

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.

[edit] Defect reports

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 the operator()'s might not be declared noexcept declared noexcept

[edit] See also

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]