C++ shared_ptr 相等运算符

发布于 2024-11-02 06:02:14 字数 618 浏览 3 评论 0原文

shared_ptr 的相等运算符定义如下:

template<class T, class U> inline bool operator==(
    shared_ptr<T> const & a, shared_ptr<U> const & b)
{
    return a.get() == b.get();
}

这似乎被破坏了。将等式转发给 a 和 b 不是更好吗 正在指着?或者这对图书馆的用户来说是一种不公平的限制(因为 他们必须提供一个相等运算符)?

如果我有一个包含shared_ptrs的map或hash_table,那么当前的定义 使得平等无法使用。例如,考虑一下

std::map<int, std::tr1::shared_ptr<T> > m1, m2;

我们是否不想检查 m1 和 m2 中每个 int 的指针是否指向相同的值?

我可以通过展平 m1, m2 来实现我自己的相等性(从每个构造集合, 一路取消引用shared_ptr)。有没有一个STL技巧可以实现这个 或者其他一些方法来巧妙地在存在shared_ptrs的情况下测试相等性?

The equality operator for shared_ptr's is defined as follows:

template<class T, class U> inline bool operator==(
    shared_ptr<T> const & a, shared_ptr<U> const & b)
{
    return a.get() == b.get();
}

This seems broken. Would it not have been better to forward the equality to what a and b
are pointing to? Or would that be an unfair restriction on users of the library (in that
they have to provide an equality operator) ?

If I have a map or a hash_table containing shared_ptrs, then the current definition
makes equality unusable. For example, consider

std::map<int, std::tr1::shared_ptr<T> > m1, m2;

Won't we want to check that the ptrs for each int in m1 and m2 are pointing to the same value ?

I can implement my own equality by flattening m1, m2 out (constructing sets from each,
dereferencing shared_ptrs along the way). Is there an STL trick that will accomplish this
or some other way to test equality in the presence of shared_ptrs neatly ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清晨说晚安 2024-11-09 06:02:14

它没有被破坏,因为 shared_ptr 在概念上是一个指针,因此它实现了指针式相等。当您测试两个指针是否相等时,您想知道它们是否指向内存中的同一位置。

It's not broken, because a shared_ptr is conceptually a pointer, therefore it implements pointer-wise equality. When you test two pointers for equality, you want to know whether they point to the same place in memory.

洒一地阳光 2024-11-09 06:02:14

我认为这个想法是比较两个shared_ptr实例与比较两个指针一样有用。如果您想要一个包含 shared_ptr 或指向对象的普通旧指针的 std::map ,则必须使用以下内容覆盖谓词:比较任一情况下所指向的对象。

在比较两个映射的情况下,您可能希望使用带有谓词的 std::equal 版本。

I think the idea is that comparing two shared_ptr instances is about as useful as comparing two pointers. If you want a std::map containing shared_ptrs or plain old pointers to objects, you'll have to override the predicate with something that compares the pointed-to objects in either case.

In the case of comparing two maps, you would probably want to use the version of std::equal that takes a predicate.

一抹淡然 2024-11-09 06:02:14

刚刚遇到一个问题,我可以使用两种类型的等价性。一组无序的shared_ptr,我希望其等价性基于所指向对象的内容。这可以使用哈希的模板特化和重载的 == 来实现。现在我有另一个容器,也包含这些指针(某种边缘关联列表),但由于我们已经知道它们是唯一的,因为我们使用了该集合,所以我们可以依赖指针等效性。尽管最初的等价性也可以工作,但在第二种情况下仅依赖指针等价性可能会更有效 - 这取决于正在比较的实例中的数据量。

那么,来回答一下问题吧。不,这不会更好,因为如何使用所提供的灵活性取决于要解决的问题。

Just ran into a problem where I could use both types of equivalence. An unordered set of shared_ptr where I wanted the equivalence to be based on the contents of the pointed to objects. This can be implemented using a template specialization of hash and an overloaded ==. Now I have another container that also contains these pointers (an edge incidence list of sorts), but since we already know they are unique because we used the set, we can rely on pointer equivalence. Although the original equivalence would also work, it might be more efficient to just rely on the pointer equivalence in the second case -- this depends on the amount of data that are in the instances being compared.

So, to answer the question. No it would not have been better, because how you use the flexibility provided depends on the problem being solved.

海之角 2024-11-09 06:02:14

您可以使用执行您想要的操作的 operator== 编写自己的 std::shared_ptr 子类。那么映射可以包含此对象

template<class t>
class my_shared_ptr : public std::shared_ptr<t>
{
    bool operator==(const std::shared_ptr<t> &other)
    {
        return (*this->get()) == (*other.get());
    }
}

,因为 operator== 不是虚拟的,这不适用于指向 shared_ptr 的指针

you can write your own subclass of std::shared_ptr with a operator== that does what you want. then the map can contain this objects

template<class t>
class my_shared_ptr : public std::shared_ptr<t>
{
    bool operator==(const std::shared_ptr<t> &other)
    {
        return (*this->get()) == (*other.get());
    }
}

because the operator== isn't virtual, this will not work with pointers to shared_ptr's

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文