简体   繁体   English

具有不完整类型的图/树实现

[英]graph/tree implementation with incomplete types

The problem has been discussed many times before. 之前已经多次讨论过这个问题。 What to do if one needs: 如果需要,该怎么办:

struct Node
{
  ::std::vector<Node> nodes_;
};

From here one gets the impression that (smart) pointers to Node* might be the canonical solution. 这里可以得到一个印象,即指向Node* (智能)指针可能是规范的解决方案。 This implies some additional indirection and a corresponding performance hit. 这意味着一些额外的间接性和相应的性能损失。 From here , we see, that libstdc++ supports ::std::vector<T> instantiations, where T is an incomplete type, but not libc++ . 这里 ,我们看到, libstdc++支持::std::vector<T>实例化,其中T是不完整的类型,但不是libc++ This is hardly portable. 这很难携带。 But one solution might be a portable ::std::vector lookalike container that supports incomplete types. 但是一个解决方案可能是一个支持不完整类型的portable ::std::vector lookalike容器。 Finally, we can do: 最后,我们可以做到:

template <::std::size_t I = 0>
struct Node
{
  ::std::vector<Node<I + 1> > nodes_;
};

template <>
struct Node<20>
{
};

Which imposes limitations on our graph/tree. 这对我们的图/树施加了限制。 Do there exist additional workarounds, due to the fact that a Node contains Node s, but is an incomplete type at the point of declaration of ::std::vector<Node> nodes_; 是否存在其他解决方法,因为Node包含Node s,但在::std::vector<Node> nodes_;的声明点处是不完整的类型::std::vector<Node> nodes_; ?

Boost containers handle incomplete types and are portable. Boost容器处理不完整的类型并且是可移植的。

So your Node can become : 所以你的节点可以成为:

#include <boost/container/vector.hpp>

struct Node
{
  boost::container::vector<Node> nodes_;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM