Boost :: recursive problem variant

Is there any way to make this work? I hope you understand that I'm trying to create a list using recursive pairs.

#include <boost/variant.hpp>
#include <utility>

struct nil {};
typedef boost::make_recursive_variant<nil, std::pair<int, boost::recursive_variant_ >>::type list_t;

int main() {
  list_t list = { 1, (list_t){ 2, (list_t){ 3, nil() } } };
  return 0;
}
+5
source share
1 answer

No. The point of boost :: variant is that it has a fixed size and does not perform dynamic allocation. So he looks like a union. The recursive boost :: variant must have infinite size in order to contain its largest possible value - obviously impossible.

You could, however, do this by passing it through a pointer. For instance:

struct nil { };

typedef boost::make_recursive_variant<nil, 
    std::pair<int, boost::scoped_ptr<boost::recursive_variant_> > >
        variant_list_int;
+4
source

All Articles