继承自 boost::variant 和模板化的 AST
Inheriting from boost::variant and templatized AST
以下代码使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 << "donen";
}
你在构造函数中获得无限递归。
第一个变体成员包含 1 个节点的聚合。因此,默认构造ast_node
s将递归初始化wrapper1
,当堆栈溢出时触底。
最简单的解决方法:
住在科里鲁
#include <array>
#include <boost/variant.hpp>
#include <iostream>
template <class Node> struct wrapper1;
template <class Node> struct wrapper2;
struct nil {};
struct ast_node;
using ast_node_base = boost::variant<nil, 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 << "donen";
}
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- boost::进程间消息队列引发错误
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- cmake如何在fedora工作站中找到boost静态库包
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- Boost Graph Library,修复节点大小
- 什么是"#include <boost/functional/hash.hpp> "?
- 基于boost的程序的静态链接——zlib问题
- C++:如何在CLion IDE中安装Boost
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何克服提升精神AST混乱
- 如何在boost beast http请求中设置http头
- Boost.Spirit将表达转换为AST
- 如何在 boost::spirit::qi 中将某些语义操作排除在 AST 之外
- boost.spirit x3 move_to and list ast member
- Boost Spirit:如何计算某些角色的出现次数,然后将结果放入AST
- 结合二元算子AST节点的Boost Spirit Qi语法
- 如何使用Boost Spirit自动规则与AST
- 继承自 boost::variant 和模板化的 AST
- boost::spirit-从随机放置的条目中解析AST样式