7

我需要比较两个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)));

我试过这段代码

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 与容器一起使用,因此根据我的经验,可以使用所有权进行比较是有道理的。

我能找到的唯一其他相关技术是比较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_hdls 是否相等?

这安全吗?

我将第一个代码修改为此

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

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

4

0 回答 0