boost::变体递归问题
有什么办法可以让这个工作吗?我希望你能明白,我正在尝试通过递归对来创建一个列表
#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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不。 boost::variant 的要点是它具有固定大小,并且不进行动态分配。这样就类似于工会了。递归 boost::variant 必须具有无限大小才能包含其最大可能值 - 显然是不可能的。
但是,您可以通过指针传递它来完成此操作。例如:
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: