std::shared_ptr::owner_before

来自cppreference.com
< cpp‎ | memory‎ | shared ptr

 
 
 
 
 
template< class T >
bool owner_before( const shared_ptr<T>& other) const;
template< class T >
bool owner_before( const std::weak_ptr<T>& other) const;
检查是否shared_ptr之前other中定义的业主(反对以价值为基础的)为了实施。两个智能指针的顺序是这样的,比较相当于只有当它们都是空的,或如果他们自己相同的对象,即使由get()得到的指针的值是不同的(例如,因为它们指向不同的子对象在同一对象)
原文:
Checks whether this shared_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_ptrstd::weak_ptr进行比较
原文:
the std::shared_ptr or std::weak_ptr to be compared
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

[编辑] 返回值

true如果*this之前otherfalse其他方式。常见的实现比较的控制块的地址.
原文:
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

[编辑] 另请参阅

提供基于所有者的,混合类型的共享指针和弱指针排序
(类模板) [编辑]