public member function template
<memory>

std::weak_ptr::owner_before

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

If the object belongs to the same owner group as x, this function returnsfalse, even if the value of their stored pointers are different (i.e., it is an alias).

This function is called by owner_less to determine its result.

参数

x
An object of a shared_ptr or weak_ptr type.

返回值

trueif the object is considered to be different from x and go before it in a strict weak order based on ownership.
false否则为 false。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// weak_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::weak_ptr<int> c (b);

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

  delete p;
  return 0;
}

输出
comparing a and c...
value-based: false
owner-based: true


另见