是否可以在没有 Boost.Fusion 的情况下使用 Boost.Spirit V2.x?
是否真的有必要用 Boost.Fusion 包装结构/类才能将它们与 Boost.Spirit V2.x(尤其是 Boost.Spirit.Qi)一起使用?我更愿意使用语义操作来分配给成员。如果我没记错的话,这就是 V1.x 中的做法...
计算器示例表明它应该仍然是可能的。到目前为止,我还没有找到一个好的方法来做到这一点。
我想看看你会如何在 员工示例。以下内容无法编译,但也许有某种方法可以使其工作:
template <typename Iterator>
struct employee_parser : qi::grammar<Iterator, employee(), ascii::space_type>
{
employee_parser() : employee_parser::base_type(start)
{
using qi::int_;
using qi::lit;
using qi::double_;
using qi::lexeme;
using ascii::char_;
quoted_string %= lexeme['"' >> +(char_ - '"') >> '"'];
start =
lit("employee")
>> '{'
>> int_[px::bind(&employee::age, qi::_val) = qi::_1] >> ','
>> quoted_string[px::bind(&employee::surname, qi::_val) = qi::_1] >> ','
>> quoted_string[px::bind(&employee::forename, qi::_val) = qi::_1] >> ','
>> double_[px::bind(&employee::salary, qi::_val) = qi::_1]
>> '}'
;
}
qi::rule<Iterator, std::string(), ascii::space_type> quoted_string;
qi::rule<Iterator, employee(), ascii::space_type> start;
};
Is it really necessary to wrap structs/classes with Boost.Fusion in order to use them with Boost.Spirit V2.x (especially Boost.Spirit.Qi)? I would much rather use semantic actions to assign to members. If my memory serves me well, this is the way it used to be done in V1.x...
The calculator example suggests that it should still be possible. So far, I haven't found a nice way to do it.
I would like to see how you would do it in the employee example. The following doesn't compile, but maybe there is some way to make it work:
template <typename Iterator>
struct employee_parser : qi::grammar<Iterator, employee(), ascii::space_type>
{
employee_parser() : employee_parser::base_type(start)
{
using qi::int_;
using qi::lit;
using qi::double_;
using qi::lexeme;
using ascii::char_;
quoted_string %= lexeme['"' >> +(char_ - '"') >> '"'];
start =
lit("employee")
>> '{'
>> int_[px::bind(&employee::age, qi::_val) = qi::_1] >> ','
>> quoted_string[px::bind(&employee::surname, qi::_val) = qi::_1] >> ','
>> quoted_string[px::bind(&employee::forename, qi::_val) = qi::_1] >> ','
>> double_[px::bind(&employee::salary, qi::_val) = qi::_1]
>> '}'
;
}
qi::rule<Iterator, std::string(), ascii::space_type> quoted_string;
qi::rule<Iterator, employee(), ascii::space_type> start;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系,我那里还有旧的融合打印材料。有趣的是,在您发布问题后,错误变得如此容易发现......:)
(既然这样有效,我一定在生产代码中犯了另一个错误。)
Nevermind, I still had the old fusion printing stuff in there. Funny how mistakes become so much easier to find after you have posted the question... :)
(Since this works, I must have made another error in the production code.)