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 Node
s, but is an incomplete type at the point of declaration of ::std::vector<Node> nodes_;
?
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_;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With