gpt4 book ai didi

c++ - Boost 堆元素句柄比较和 MSVC 迭代器调试工具

转载 作者:行者123 更新时间:2023-11-30 05:10:34 27 4
gpt4 key购买 nike

(最初在 boost-users ML 上提问: [heap] Singular handle issue with MSVC iterator debug facilities )

使用 VS2017(版本 15.2)和 Boost 1.64,我正在编译一个使用 boost::heap::d_ary_heap 的程序.

在调试配置(默认 _ITERATOR_DEBUG_LEVEL )中,当堆上的项目句柄与 handle_type 的默认构造实例进行比较时,我观察到运行时的问题.

顺便说一句,handle_type编译基于std::list::iteratorboost::heap::detail::priority_queue_mutable_wrapper 中所定义.

问题是 MSVC 迭代器调试工具中断执行

File: c:\program files (x86)\microsoft visual
studio\2017\professional\vc\tools\msvc\14.10.25017\include\list
Line: 290
Expression: list iterators incompatible

AFAIU,handle_type{};似乎产生句柄 h包装单个迭代器的对象。

下面,我复制了最小的示例来重现该问题。

#include <cstdint>
#include <functional>
#include <utility>
#include <boost/heap/d_ary_heap.hpp>

using NodeID = std::uint32_t;
using EdgeWeight = std::int32_t;

using HeapData = std::pair<EdgeWeight, NodeID>;
using HeapContainer = boost::heap::d_ary_heap<
HeapData,
boost::heap::arity<4>,
boost::heap::mutable_<true>,
boost::heap::compare<std::greater<HeapData>>>;
using HandleType = HeapContainer::handle_type;

int main()
{
HeapContainer heap;
auto const handle = heap.push(std::make_pair(100, 1));
handle == HandleType{}; // _DEBUG_ERROR("list iterators incompatible");

return 0;
}

调试示例时,Visual C++ 调试器不显示迭代器由句柄包装为默认构造的空节点指针。相反,它显示垃圾:((???, ???), ???)

这是使用 boost::heap::d_ary_heap 时已知的 MSVC 缺陷吗?或高于 handle_type真的被误用了吗?

最佳答案

我已经阅读了 MSVC 库实现中的迭代器检查实现,并且必须得出结论,您无法通过默认构造(拥有的容器将始终不匹配)获得可检查的可比较迭代器。

措辞不同:默认构造的句柄确实是单数的,但它们在 MSVC 上是如此的单数,以至于它们只能与另一个单数实例进行检查比较。这很好:

 HandleType{} == HandleType{}

为了获得可靠的“不存在”句柄,我会使用结束迭代器句柄:

Live On Coliru

#include <cstdint> 
#include <functional>
#include <boost/heap/d_ary_heap.hpp>
using NodeID = std::uint32_t;
using EdgeWeight = std::int32_t;
using HeapData = std::pair<EdgeWeight, NodeID>;

using HeapContainer = boost::heap::d_ary_heap<
HeapData,
boost::heap::arity<4>,
boost::heap::mutable_<true>,
boost::heap::compare<std::greater<HeapData>>>;

using HandleType = HeapContainer::handle_type;

int main() {
HeapContainer heap;
auto const none = heap.s_handle_from_iterator(heap.end());
auto const handle = heap.push(std::make_pair(100, 1));
assert(handle != none);
}

(当然静态可以作为HeapContainer::s_handle_from_iterator调用)

关于c++ - Boost 堆元素句柄比较和 MSVC 迭代器调试工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45608426/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com