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 returns false, 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.
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 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;
}
|
Output:
comparing a and c...
value-based: false
owner-based: true
|
See also
- weak_ptr::lock
- Lock and restore weak_ptr (public member function)
- owner_less
- Owner-based less-than operation (class template)