我想重用一个规则,并在它前面加上一个关键字。所以我的规则是这样的:
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <string>
namespace qi = boost::spirit::qi;
struct A {
int index;
std::string name;
};
BOOST_FUSION_ADAPT_STRUCT(::A, (int, index)(std::string, name))
struct B {
A data;
};
BOOST_FUSION_ADAPT_STRUCT(::B, (A, data))
int main() {
typedef std::string::const_iterator iterator_type;
qi::rule<iterator_type, A()> a_rule = qi::int_ > qi::lit(",") > *(qi::char_);
qi::rule<iterator_type, B()> b_rule = qi::lit("(") > a_rule > qi::lit(")");
qi::rule<iterator_type, B()> bad_rule = qi::lit("keyword") > b_rule;
return 0;
}
这不会编译,因为编译器想要A
从B
(参考 bad_rule 的定义)创建:
C:/tc/gcc_x64_4.8.1_win32_seh_rev1/mingw64/my/src/boost_1_54_0/boost/spirit/home/qi/detail/assign_to.hpp:152:18: error: no matching function for call to 'A::A(const B&)'
attr = static_cast<Attribute>(val);
^
但是,将其更改struct B
为不那么简单,使其工作:
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <string>
namespace qi = boost::spirit::qi;
struct A {
int index;
std::string name;
};
BOOST_FUSION_ADAPT_STRUCT(::A, (int, index)(std::string, name))
struct B {
A data;
int dummy;
};
BOOST_FUSION_ADAPT_STRUCT(::B, (A, data)(int, dummy))
int main() {
typedef std::string::const_iterator iterator_type;
qi::rule<iterator_type, A()> a_rule = qi::int_ > qi::lit(",") > *(qi::char_);
qi::rule<iterator_type, B()> b_rule = qi::lit("(") > a_rule > qi::lit(")") > qi::attr(0);
qi::rule<iterator_type, B()> bad_rule = qi::lit("keyword") > b_rule;
return 0;
}
任何想法,如何在没有这种解决方法的情况下相处以及这里发生了什么?