std::weak_p...owner_before(3)
β¨ NAME β¨
bestie, welcome to the manual page for std::weak_p...owner_before(3) β aka std::weak_ptr::owner_before π
Itβs literally std::weak_ptr::owner_before, no cap π
πΈ Synopsis πΈ
template< class Y >
bool owner_before( const weak_ptr<Y>& other ) const noexcept;
template< class Y >
bool owner_before( const std::shared_ptr<Y>& other ) const noexcept;
Okay so this checks whether this weak_ptr precedes other in an implementation defined owner-based order β thatβs owner-based, not value-based, bestie π
The order is set up so that two smart pointers compare equivalent only if they are both empty, or if they both own the same object. And yes, that still holds even if the values of the pointers you get from get() are different β like, say, because they point at different subobjects within the same object. Iconic behavior, honestly π«Ά
This ordering is what makes shared and weak pointers usable as keys in associative containers, typically through std::owner_less β¨
π Parameters π
otherβ thestd::shared_ptrorstd::weak_ptrto be compared π
π Return value π
true if *this precedes other, false otherwise. Common implementations compare the addresses of the control blocks π§
π¦ Example π¦
// Run this code
#include <iostream>
#include <memory>
struct Foo
{
int n1;
int n2;
Foo(int a, int b) : n1(a), n2(b) {}
};
int main()
{
auto p1 = std::make_shared<Foo>(1, 2);
std::shared_ptr<int> p2(p1, &p1->n1);
std::shared_ptr<int> p3(p1, &p1->n2);
std::cout << std::boolalpha
<< "p2 < p3 " << (p2 < p3) << '\n'
<< "p3 < p2 " << (p3 < p2) << '\n'
<< "p2.owner_before(p3) " << p2.owner_before(p3) << '\n'
<< "p3.owner_before(p2) " << p3.owner_before(p2) << '\n';
std::weak_ptr<int> w2(p2);
std::weak_ptr<int> w3(p3);
std::cout
// << "w2 < w3 " << (w2 < w3) << '\n' // won't compile
// << "w3 < w2 " << (w3 < w2) << '\n' // won't compile
<< "w2.owner_before(w3) " << w2.owner_before(w3) << '\n'
<< "w3.owner_before(w2) " << w3.owner_before(w2) << '\n';
}
Output:
p2 < p3 true
p3 < p2 false
p2.owner_before(p3) false
p3.owner_before(p2) false
w2.owner_before(w3) false
w3.owner_before(w2) false
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- LWG 2083 β Applied to:
C++11β Behavior as published:owner_beforewas not declaredconstβ Correct behavior: declaredconst - LWG 2942 β Applied to:
C++11β Behavior as published:owner_beforewas not declarednoexceptβ Correct behavior: declarednoexcept
π See also π
owner_lessβ provides mixed-type owner-based ordering of shared and weak pointers (C++11) (class template) π
mwah π β source/version and date, bestie: http://cppreference.com, version 2024.06.10, manual page std::weak_p...owner_before(3). Thatβs the footer, and yes, it ate π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms