boost :: variant递归麻烦 [英] boost::variant recursive trouble

查看:193
本文介绍了boost :: variant递归麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让这项工作?我希望你会得到的想法,我想通过递归对创建一个列表

is there any way to make this work? I hope you'll get the idea, I'm trying to create a list by means of 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;
}


推荐答案

boost :: variant的一点是它有固定的大小,不做动态分配。这样,它就像一个联合。递归boost ::变体必须具有无限大小,以便包含其最大可能的值 - 显然是不可能的。

No. The point of a boost::variant is that it has a fixed size, and does not do dynamic allocation. In this way it's similar to a union. A recursive boost::variant would have to have infinite size in order to contain its largest possible value - clearly impossible.

然而,你可以通过传递它指针。例如:

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

struct nil { };

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

这篇关于boost :: variant递归麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆