c++ - 简单 X3 语法出现难以理解的编译错误

标签 c++ visual-c++ boost-spirit boost-spirit-x3

我尝试使用 boostspirit x3 实现一个非常简单的语法,但没有成功。

它无法编译,并且由于库中使用的所有模板和复杂概念(我知道,它更像是一个“ header ”),编译错误消息太长而难以理解。

我尝试注释部分代码以缩小罪魁祸首的范围,但没有成功,因为它分为几个部分,无论如何我都没有看到任何错误。

Edit2:第一条错误消息确实位于 push_front_impl.hpp 中,突出显示:
::REQUESTED_PUSH_FRONT_SPECIALISATION_FOR_SEQUENCE_DOES_NOT_EXIST::*

我怀疑关键字auto或者p2语句与ulong_long......但没有信心。 需要你们的帮助...灵魂精英!

下面是重现编译错误的最小代码片段。 编辑:使用boost 1.70和Visual Studio 2019 v16.1.6

#include <string>
#include <iostream>
#include "boost/spirit/home/x3.hpp"
#include "boost/spirit/include/support_istream_iterator.hpp"

int main(void)
{
       std::string input = \
             "\"nodes\":{ {\"type\":\"bb\", \"id\" : 123456567, \"label\" : \"0x12023049\"}," \
                         "{\"type\":\"bb\", \"id\" : 123123123, \"label\" : \"0x01223234\"}," \
                         "{\"type\":\"ib\", \"id\" : 223092343, \"label\" : \"0x03020343\"}}";
       std::istringstream iss(input);
       namespace x3 = boost::spirit::x3;
       using x3::char_;
       using x3::ulong_long;
       using x3::lit;
 
       auto q = lit('\"'); /* q => quote */
 
       auto p1 = q >> lit("type") >> q >> lit(':') >> q >> (lit("bb") | lit("ib")) >> q;
       auto p2 = q >> lit("id") >> q >> lit(':') >> ulong_long;
       auto p3 = q >> lit("label") >> q >> lit(':') >> q >> (+x3::alpha) >> q;
       auto node =  lit('{') >> p1 >> lit(',') >> p2 >> lit(',') >> p3 >> lit('}');
       auto nodes = q >> lit("nodes") >> q >> lit(':') >> lit('{') >> node % lit(',') >> lit('}');
 
       boost::spirit::istream_iterator f(iss >> std::noskipws), l{};
       bool b = x3::phrase_parse(f, l, nodes, x3::space);
 
       return 0;
}

最佳答案

这是一个已知的 MPL 限制( Issue with X3 and MS VS2017https://github.com/boostorg/spirit/issues/515 )+ MSVC/ICC 编译器实现的错误/差异( https://github.com/boostorg/mpl/issues/43 )。

我在没有使用 MPL 的情况下重写了一个有问题的部分( https://github.com/boostorg/spirit/pull/607 ),它将在 Boost 1.74 中发布,在那之前您应该能够使用以下方法解决问题:

#define BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS
#define BOOST_MPL_LIMIT_VECTOR_SIZE 50

或者,您可以将语法的不同部分包装成规则,这将减少序列解析器链。


请注意 q >> lit("x") >> q >> lit(':') >> ... 可能不是您真正想要的,它(带有船长) 将允许解析 "x ":。如果您不想这样做,只需使用 lit("\"x\"") >> lit(':') >> ...

关于c++ - 简单 X3 语法出现难以理解的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63080719/

相关文章:

c++ - 使用什么数据结构来存储游戏对象的基于 2D 单元的 map ?

c++ - 为什么 Avira 将 "CoCreateInstance()"视为恶意软件?

c++ - Boost Spirit 2 - 符号扩展和回溯

c++ - CMAKE 链接器没有找到库;但是使用 find_library 找到了库

c++ - c++ 中 vector 的 bad_alloc 异常

c++ - C++ sizeof-需要帮助理解

c++ - "break when an exception is void"是什么意思?

c++ - Visual C++ 应用程序中的 SQL Server 数据库

c++ - 新标准的特性对 C++11 中的 boost 库实现有重大影响吗?

c++ - Spirit 无法将属性分配给单元素结构(或 fusion 序列)