比较connection_hdl(weak_ptr)是否相等

7 c++ comparison weak-ptr websocket++

我需要比较两个websocket ++ connection_hdl:

// Create a weak pointer on the heap using that shared_ptr.
// Cast that weak pointer to void* and manage it using another shared_ptr
// connection_hdl hdl(reinterpret_cast<void*>(new connection_weak_ptr(con)));
Run Code Online (Sandbox Code Playgroud)

我试过这段代码

template <typename T, typename U>
inline bool equals(const connection_hdl<T>& t, const connection_hdl<U>& u)
{
    return !t.owner_before(u) && !u.owner_before(t);
}
Run Code Online (Sandbox Code Playgroud)

但编译器抱怨说connection_hdl is not a template.

可以修改上面的代码来比较connection_hdls吗?connection_hdls可以通过使用owner_less与容器一起使用,所以在我缺乏经验的情况下,可以使用所有权来进行比较.

我能找到的唯一其他相关技术是比较weak_ptrs集

bool result = !(std::lexicographical_compare(set1.begin(), set1.end(),
                                     set2.begin(), set2.end(),
                                     set1.value_comp()) ||
            std::lexicographical_compare(set2.begin(), set2.end(),
                                     set1.begin(), set1.end(),
                                     set1.value_comp()));
Run Code Online (Sandbox Code Playgroud)

这似乎与我的需求很接近,但由于我的经验不足,我不能确定或修改那些符合我意图的代码.

如何比较两个connection_hdl的相等性?

这样安全吗?

我修改了第一个代码

bool connections_equal(connection_hdl t, connection_hdl u)
{
    return !t.owner_before(u) && !u.owner_before(t);
}
Run Code Online (Sandbox Code Playgroud)

它编译.除了weak_ptr空虚警告之外,这是否安全?我的经验不足使我无法确定,而我的有限经验告诉我,仅仅因为它编译,并不意味着它会按预期工作.