我正在尝试使用 Howard Hinnant 的 stack_alloc
使用 boost rtrees,如以下示例所示:
#include "stack_alloc.h"
#include <boost/geometry/index/rtree.hpp>
using NodePoint = boost::geometry::model::point<double, 2, boost::geometry::cs::cartesian>;
using Linear = boost::geometry::index::linear<8, 2>;
using RTree =
boost::geometry::index::rtree<NodePoint, Linear, boost::geometry::index::indexable<NodePoint>,
boost::geometry::index::equal_to<NodePoint>,
stack_alloc<NodePoint, 100>>;
int main()
{
RTree my_tree{};
return 0;
}
这无法通过相当大的模板错误堆栈进行编译。我认为问题的核心是:
/usr/local/include/boost/geometry/index/detail/rtree/node/variant_static.hpp:26:7: error: invalid use of incomplete type 'class boost::geometry::index::detail::rtree::allocators, 100>, boost::geometry::model::point, boost::geometry::index::linear<8, 2>, boost::geometry::model::box >, boost::geometry::index::detail::rtree::node_variant_static_tag>'
这是 coliru 上的完整示例有完整的错误。
这里有什么问题吗?
我尝试将 stack_alloc
与各种 boost 集合一起使用,例如 boost::container::static_vector
和 boost::container::map
以及那些工作得很好。
我还尝试使用来自 this SO reply 的另一个 stack_allocator
实现并得到了同样的错误。
此外,我知道 Howard Hinnant 有一个更新的实现,即 short_alloc
.我试过使用它,但这个实现没有默认的构造函数,需要我们在构造时提供存储。由于 boost
将分配器作为模板参数并在内部对其进行实例化,因此我找不到实现此功能的方法,但如果有方法,我会很乐意使用它。 stack_alloc
和/vs short_alloc
的更多信息:1 , 2 , 3
最佳答案
问题的核心本质上是循环依赖。
构建RTree
导致 rtree<...>
包含 typedef 的模板实例化 node_pointer = allocators_type::node_pointer
,这会触发 allocators_type
的实例化,即 detail::rtree::allocators<...>
,其基类为 detail::rtree::node_alloc<...>
,在其定义中将分配器重新绑定(bind)到节点类型。节点类型是 detail::rtree::variant_leaf<...>
的变体和 detail::rtree::variant_internal_node<...>
.
但是stack_alloc
需要 sizeof(T)
, 所以这两个模板都包含在 variant
中类型被实例化,并且在实例化时 variant_internal_node
, 它需要 Allocators::node_pointer
, 所以 Allocators
必须实例化,但这不正是我们正在实例化的吗!
我建议尝试 short_alloc
并将分配器传递给容器。因为它将存储与分配器类型分开,所以它不应该要求模板类型的完整性,打破循环。
关于c++ - Hinnant 的堆栈分配器与 boost rtrees : compilation failure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57368793/