public member function template
<memory>

std::shared_ptr::owner_before

template <class U> bool owner_before (const shared_ptr<U>& x) const;
template <class U> bool owner_before (const weak_ptr<U>& x) const;
Owner-based ordering
Returns whether the object is considered to go before x following a strict weak owner-based order.

Unlike the operator< overload, this ordering takes into consideration the shared_ptr's owned pointer, and not the stored pointer in such a way that two of these objects are considered equivalent (i.e., this function returns false no matter the order of the operands) if they both share ownership, or they are both empty, even if their stored pointer value are different.

The stored pointer (i.e., the pointer the shared_ptr object dereferences to) may not be 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).

This function is called by owner_less to determine its result.

Parameters

x
An object of a shared_ptr or weak_ptr type.

Return value

true if the object is considered to be different from x and go before it in a strict weak order based on ownership.
false otherwise.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// shared_ptr::owner_before
#include <iostream>
#include <memory>

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

  std::shared_ptr<int> a (new int (20));
  std::shared_ptr<int> b (a,p);  // alias constructor

  std::cout << "comparing a and b...\n" << std::boolalpha;
  std::cout << "value-based: " << ( !(a<b) && !(b<a) ) << '\n';
  std::cout << "owner-based: " << ( !a.owner_before(b) && !b.owner_before(a) ) << '\n';

  delete p;
  return 0;
}


Output:
comparing a and b...
value-based: false
owner-based: true

See also