继承自boost :: variant和templatized AST

时间:2016-08-15 07:12:09

标签: c++ boost boost-variant

以下代码crashes GCC并且无法使用Clang进行编译。它有什么问题?

#include <boost/variant.hpp>
#include <array>
#include <iostream>


template<class Node>
struct wrapper1;
template<class Node>
struct wrapper2;

struct ast_node;
using ast_node_base = boost::variant<boost::recursive_wrapper<wrapper1<ast_node>>, boost::recursive_wrapper<wrapper2<ast_node>>>;

struct ast_node : ast_node_base
{
    using ast_node_base::ast_node_base;
};

template<class Node>
struct wrapper1
{
    std::array<Node, 1> children;
};

template<class Node>
struct wrapper2
{
    std::array<Node, 2> children;
};


int main()
{
    ast_node node;
    std::cout << "done\n";
}

1 个答案:

答案 0 :(得分:2)

你在构造函数中得到无限递归。

第一个变体成员本身包含1个节点的聚合。因此,默认构造的valac将递归初始化ast_node,当堆栈溢出时,它会触底。

最简单的解决方法:

Live On Coliru

wrapper1