class template
<memory>

std::owner_less

template <class Ptr> struct owner_less;template <class T> struct owner_less<shared_ptr<T>>;template <class T> struct owner_less<weak_ptr<T>>;
Owner-based less-than operation
This class defines function objects that perform an owner-based comparison between shared_ptr and/or weak_ptr objects.

This is a replacement for less (or operator<) to be used for these types when sorting needs to be based on their owned pointer instead of their stored pointer (which is what is compared by operator<).

The stored pointer of a shared_ptr object (i.e., the pointer it dereferences to) may be different from the owned pointer (i.e., the pointer deleted on object destruction) if the shared_ptr object is an alias (alias-constructed objects and their copies).

The comparison performed by owner_less::operator() relies on member function owner_before of either shared_ptr or weak_ptr. owner_less is defined with an interface that mimics a binary_function, but with additional overloads. It is implemented with the same behavior as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template<class P> struct owner_less;  // not defined

template<class T> struct owner_less <shared_ptr<T>>
{
  typedef bool result_type;
  typedef shared_ptr<T> first_argument_type;
  typedef shared_ptr<T> second_argument_type;
  bool operator() (const shared_ptr<T>& x, const shared_ptr<T>& y) const {return x.owner_before(y);}
  bool operator() (const shared_ptr<T>& x, const weak_ptr<T>& y) const {return x.owner_before(y);}
  bool operator() (const weak_ptr<T>& x, const shared_ptr<T>& y) const {return x.owner_before(y);}
};

template<class T> struct owner_less <weak_ptr<T>>
{
  typedef bool result_type;
  typedef weak_ptr<T> first_argument_type;
  typedef weak_ptr<T> second_argument_type;
  bool operator() (const weak_ptr<T>& x, const weak_ptr<T>& y) const {return x.owner_before(y);}
  bool operator() (const shared_ptr<T>& x, const weak_ptr<T>& y) const {return x.owner_before(y);}
  bool operator() (const weak_ptr<T>& x, const shared_ptr<T>& y) const {return x.owner_before(y);}
};


Template parameters

Ptr
The type of the managed pointers to be ordered according to owned resource, aliased as member types first_argument_type and second_argument_type.
T
The type of object pointed by the managed pointer type.

Member types

The following aliases are member types of owner_less.

member typedefinitionnotes
result_typeboolThe result of the operation
first_argument_typePtrThe type of the first argument
second_argument_typePtrThe type of the second argument

Member functions

operator()
Returns true if the first argument is considered to go before the second argument in a strict weak ordering based on their owned pointers.
The function uses shared_ptr::owner_before and/or weak_ptr::owner_before to determine this order.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// owner_less example
#include <iostream>
#include <memory>
#include <set>

int main ()
{
  int * p = new int (10);

  std::shared_ptr<int> a (new int (20));
  std::shared_ptr<int> b (a,p);  // alias constructor: co-owns a, points to p

  // standard set container: cannot contain duplicates.
  std::set < std::shared_ptr<int> > value_based;  // uses std::less
  std::set < std::shared_ptr<int>, std::owner_less<std::shared_ptr<int>> > owner_based;

  value_based.insert (a);
  value_based.insert (b);  // ok, different value

  owner_based.insert (a);
  owner_based.insert (b);  // overwrites (same owned pointer)

  std::cout << "value_based.size() is " << value_based.size() << '\n';
  std::cout << "owner_based.size() is " << owner_based.size() << '\n';

  delete p;
  return 0;
}

Output:
value_based.size() is 2
owner_based.size() is 1


See also