I need to create a structure that has a vector<self> as a member variable. Boost offers 2 mechanisms that achieve this:
1 – using boost::recursive_wrapper i.e.:
struct filter
{
uint32_t id;
std::vector< boost::recursive_wrapper< filter > > childFilters;
};
2 – using boost::container i.e.:
struct filter
{
uint32_t id;
boost::container::vector< filter > childFilters;
};
Is there any advantage with each technique? The second boost::container option involves less syntax, and I guess it uses a technique similar to the boost::recursive_wrapper internally.
The better choice is to use
boost::container::vector, because it allows direct access to the type stored in the vector.The extra layer of indirection provided by
boost::recursive_wrapperthat allows the declaration of astd::vector<self>makes code manipulating the vector more cumbersome, because it is necessarygetto access the actual type being held.i.e.
With
boost::container::vectorthe example becomes: