比较connection_hdl(weak_ptr)是否相等

时间:2014-05-11 19:26:14

标签: 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)));

我尝试了this code

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);
}

但编译器抱怨connection_hdl is not a template

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

我能找到的唯一其他相关technique用于比较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()));

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

如何比较两个connection_hdl是否相等?

这样安全吗?

我将第一个代码修改为此

bool connections_equal(connection_hdl t, connection_hdl u)
{
    return !t.owner_before(u) && !u.owner_before(t);
}

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

0 个答案:

没有答案
相关问题