将解析器分配给自动变量 [英] Assigning parsers to auto variables
问题描述
精神解析器不是用于 auto
?
一个简单的解析器在传递给 qi :: parse()
inline,但通过 auto
变量传递时会与segfault一起崩溃:
A simple parser works fine when passed to qi::parse()
inline, but crashes with segfault if passed via an auto
variable:
#include <cstdio>
#include <string>
#include <boost/spirit/include/qi.hpp>
using namespace std;
namespace qi = boost::spirit::qi;
int main()
{
string line = "[z]";
auto bracketed_z = '[' >> +qi::char_('z') >> ']';
auto p = line.cbegin();
printf("%d", qi::parse(p, line.cend(), '[' >> +qi::char_('z') >> ']')); // Works
p = line.cbegin();
printf("%d", qi::parse(p, line.cend(), bracketed_z)); // Crashes
}
使用g ++ - 4.8和VC13进行重复。
Reproes with g++-4.8 and VC13.
更新:在原始代码中修正了错误( p
在第二次调用 parse()
)。
Update: an error was fixed in the original code (p
was not re-initialized before the second call to parse()
).
推荐答案
Spirit Parsers不适用于 auto
在Spirit V2中。
Spirit Parsers are not designed to be used with auto
in Spirit V2.
这是因为底层的Proto表达式模板保存对临时实体的引用。
This is because the underlying Proto expression templates hold references to the temporaries.
您可以使用
-
qi :: copy()
(存在于boost_1_55_0后的中继线中,此时不是任何发布的版本) -
boost :: proto :: deep_copy
- 或BOOST_SPIRIT_AUTO(首先创建此处)
qi::copy()
(existing in the trunk after boost_1_55_0, not in any released version at this time)boost::proto::deep_copy
- or BOOST_SPIRIT_AUTO (first coined here)
我已经在SO上更多地写这些内容: http://stackoverflow.com/search?q=user%3A85371+deep_copy ,具体来说,这是:
I've written about these things more often on SO: http://stackoverflow.com/search?q=user%3A85371+deep_copy, specifically, this:
- boost spirit V2 qi bug associated with optimization level
Boost Spirit X3不会有这个限制。
Boost Spirit X3 will not have this limitation.
这篇关于将解析器分配给自动变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!