【发布时间】:2017-10-20 17:31:53
【问题描述】:
我正在尝试使用 Boost 1.65.1 中的 Spirit X3 来制作解析器。我已将我的问题简化为以下结构更简单的较小示例:
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <iostream>
#include <vector>
struct MyPair {
MyPair(int x, int y) : mx(x), my(y) {};
//MyPair() {} // No default constructor - neither needed nor wanted.
int mx;
int my;
};
/*
BOOST_FUSION_ADAPT_STRUCT(
MyPair,
(int, mx)
(int, my)
)
*/
int main()
{
using boost::spirit::x3::int_;
using boost::spirit::x3::parse;
std::vector<MyPair> pairs;
char const *first = "11:22,33:44,55:66", *last = first + std::strlen(first);
auto pair = [&](auto& ctx) { return MyPair(1, 2); };
bool parsed_some = parse(first, last, ((int_ >> ':' >> int_)[pair]) % ',', pairs);
if (parsed_some) {
std::cout << "Parsed the following pairs" << std::endl;
for (auto& p : pairs) {
std::cout << p.mx << ":" << p.my << std::endl;
}
}
return 0;
}
我不想为我的类型(此处为 MyPair)添加默认构造函数。如果没有默认构造函数,我会收到以下错误:
'MyPair::MyPair': no appropriate default constructor available ...\boost\utility\value_init.hpp
但我不想将我的结构更改为具有默认构造函数。假设,我确实添加了一个,我得到的最后一个错误是:
binary '=': no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) TestParsing ...\x3\support\traits\move_to.hpp
但是由于我在语义动作中手动构造了属性,所以我不明白为什么需要进行融合定义。 (注意,目前它使用硬编码值,直到我解决了这个问题,然后得到正确的值)。
如何使用 Spirit X3 在没有默认构造函数的情况下构造用户定义类型的属性?
【问题讨论】:
标签: c++ boost-spirit boost-spirit-x3