我有以下解析规则:
filter = (input >> (qi::repeat(0,2)[char_(';') >> input]))input是一个规则,它返回一个std::vector<int> (向量),我将简称为vec。
问题是:filter规则将返回什么复合属性?
我试过:
fusion::vector <vec,std::vector <fusion::vector <char,vec> > >但是失败了,我不知道为什么。
发布于 2013-07-26 08:22:44
解析器表达式的属性类型为相当的 well-记录在案。但这可能会让人迷失方向和消磨时间。
下面是一个技巧:发送一个哨兵来检测属性类型:
struct Sniffer
{
typedef void result_type;
template <typename T>
void operator()(T const&) const { std::cout << typeid(T).name() << "\n"; }
};然后使用折叠解析器表达式。
(input >> (qi::repeat(0,2)[qi::char_(';') >> input])) [ Sniffer() ]将转储:
N5boost6fusion7vector2ISt6vectorIsSaIsEES2_INS1_IcS4_EESaIS5_EEEEc++filt -1将告诉您哪个表示:
boost::fusion::vector2<
std::vector<short, std::allocator<short> >,
std::vector<boost::fusion::vector2<char, std::vector<short, std::allocator<short> > >,
std::allocator<boost::fusion::vector2<char, std::vector<short, std::allocator<short> > >
> >
>在Coliru:http://coliru.stacked-crooked.com/view?id=3e767990571f8d0917aae745bccfa520-5c1d29aa57205c65cfb2587775d52d22上观看现场直播
boost::fusion::vector2<std::vector<short, std::allocator<short> >, std::vector<std::vector<short, std::allocator<short> >, std::allocator<std::vector<short, std::allocator<short> > > > >它可能非常复杂,部分原因是因为char_(";")可能是';' (或者更确切地说是lit(';'))。与此相抗衡(Coliru)
boost::fusion::vector2<
std::vector<short, ... >,
std::vector<std::vector<short, std::allocator<short> >, ... > >这应该能回答你的问题。
Sidenotes:解析事物
不要低估自动属性在精神中的传播。通常,您不必费心于属性的确切公开类型。相反,依赖于(许多)属性转换,这些转换将它们分配给所提供的属性引用。
我相信你在精神上认识列表操作员(%)吗?我将向您展示如何使用它而不用再费劲地说:
vector<vector<short>> data;
qi::parse(f, l, qi::short_ % ',' % ';', data);现在,如果需要强制执行这样一个事实,即它可能是1-3个元素,则可以使用带有凤凰操作的eps来断言最大大小:
const string x = "1,2,3;2,3,4;3,4,5";
auto f(begin(x)), l(end(x));
if (qi::parse(f, l,
(qi::eps(phx::size(qi::_val) < 2) > (qi::short_ % ',')) % ';'
, data))
{
cout << karma::format(karma::short_ % ',' % ';', data) << "\n";
}
cout << "remaining unparsed: '" << std::string(f,l) << "'\n";指纹:
1,2,3;2,3,4
remaining unparsed: ';3,4,5'https://stackoverflow.com/questions/17870613
复制相似问题