std::weak_ptr::owner_before
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
template< class T > bool owner_before( const weak_ptr<T>& other) const; |
||
template< class T > bool owner_before( const std::shared_ptr<T>& other) const; |
||
检查是否
weak_ptr
之前other
中定义的业主(反对以价值为基础的)为了实施。两个智能指针的顺序是这样的,比较相当于只有当它们都是空的,或如果他们自己相同的对象,即使由get()得到的指针的值是不同的(例如,因为它们指向不同的子对象在同一对象)原文:
Checks whether this
weak_ptr
precedes other
in implementation defined owner-based (as opposed to value-based) order. The order is such that two smart pointers compare equivalent only if they are both empty or if they both own the same object, even if the values of the pointers obtained by get() are different (e.g. because they point at different subobjects within the same object)这种顺序是用来共享和弱指针可用键关联容器,通常是通过std::owner_less.
原文:
This ordering is used to make shared and weak pointers usable as keys in associative containers, typically through std::owner_less.
目录 |
[编辑] 参数
other | - | std::shared_ptr或std::weak_ptr进行比较
原文: the std::shared_ptr or std::weak_ptr to be compared |
[编辑] 返回值
true如果*this之前
other
,false其他方式。常见的实现比较的控制块的地址.原文:
true if *this precedes
other
, false otherwise. Common implementations compare the addresses of the control blocks.[编辑] 为例
#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'; }
输出:
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
[编辑] 另请参阅
(C++11) |
混合型业主的共享和弱指针的顺序 原文: provides mixed-type owner-based ordering of shared and weak pointers (类模板) |