Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

graph/tree implementation with incomplete types

Tags:

c++

c++11

stl

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. 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++. This is hardly portable. But one solution might be a portable ::std::vector lookalike container that supports incomplete types. 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 Nodes, but is an incomplete type at the point of declaration of ::std::vector<Node> nodes_;?

like image 764
user1095108 Avatar asked Nov 24 '13 15:11

user1095108


1 Answers

Boost containers handle incomplete types and are portable.

So your Node can become :

#include <boost/container/vector.hpp>

struct Node
{
  boost::container::vector<Node> nodes_;
};
like image 191
Drax Avatar answered Oct 01 '22 14:10

Drax