Ale*_*kis 7 c++ boost memory-management allocator c++11
我试图将Howard stack_allocHinnant 's 与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;
}
Run Code Online (Sandbox Code Playgroud)
无法使用相当大的模板错误堆栈进行编译。我认为问题的核心是:
/usr/local/include/boost/geometry/index/detail/rtree/node/variant_static.hpp:26:7:错误:无效使用了不完整的类型'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,效果很好。我还尝试stack_allocator从此SO答复中使用另一种实现,并得到相同的错误。
此外,我知道Howard Hinnant有一个更新的实现,即short_alloc。我尝试使用它,但是此实现没有默认的ctor,要求我们在构建时提供存储。由于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<...>,该实例在其定义中将分配器重新绑定到节点类型。该节点类型是其变体detail::rtree::variant_leaf<...>和detail::rtree::variant_internal_node<...>。
但是stack_alloc需要sizeof(T),因此variant类型中包含的两个模板都将被实例化,而在实例化时variant_internal_node,它需要被实例化,Allocators::node_pointer因此Allocators必须实例化,但这不是我们正在实例化的过程!
我建议尝试short_alloc将分配器传递给容器。因为它将存储与分配器类型分开,所以它不要求模板类型完整无缺。