使用boost :: spirit时无法在boost :: variant中转换boost :: fusion结构

时间:2012-12-06 20:14:04

标签: c++ boost boost-spirit boost-variant boost-fusion

一天中的好时光!

我编写了这段代码并期望它能够被编译。

#include <string>

#include <boost/spirit/include/qi.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <boost/fusion/include/adapted.hpp>

struct ParsedThunk
{
    typedef boost::variant<std::string, boost::tuple<boost::recursive_wrapper<ParsedThunk>, boost::recursive_wrapper<ParsedThunk> > > ParsedThunkEntity;
    ParsedThunkEntity _entity;
};

BOOST_FUSION_ADAPT_STRUCT(
    ParsedThunk,
    (ParsedThunk::ParsedThunkEntity, _entity)
)

class ThunkParser
{
protected:
    boost::spirit::qi::rule<std::string::iterator, ParsedThunk(), boost::spirit::ascii::space_type> _thunkRule;

public:
    ThunkParser()
    {
        _thunkRule %= (
            (boost::spirit::qi::lexeme[+(boost::spirit::qi::char_ - boost::spirit::qi::char_(" =()"))]) | 
            (boost::spirit::qi::lit("(") >> _thunkRule >> _thunkRule >> boost::spirit::qi::lit(")")) | 
            (_thunkRule >> _thunkRule)
            ) >> boost::spirit::qi::eps;
    }
};

int main(void)
{
    return 0;
}

但是当我用clang或GCC编译它时,我得到一个错误。最有趣的部分是:

/usr/include/boost/variant/detail/initializer.hpp:89:24: note: candidate function not viable: no known conversion from 'const boost::fusion::vector2<ParsedThunk, ParsedThunk>' to 'const
      boost::tuples::tuple<boost::recursive_wrapper<ParsedThunk>, boost::recursive_wrapper<ParsedThunk>, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type,
      boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>' for 2nd argument;
            static int initialize(void* dest, param_T operand)

我使用clang 3.1或gcc 4.7并提升1.52,但我认为没关系。 :)

如何编辑这段代码以使其编译?

更新

好吧,我刚注意到我指定的语法是左递归的,因此递归下行解析器在处理输入时可能不会停止。这可能是编译问题的原因吗?

1 个答案:

答案 0 :(得分:3)

修复您的变体类型:

typedef boost::variant<
            std::string,
            boost::recursive_wrapper<boost::tuple<ParsedThunk, ParsedThunk> >
        > ParsedThunkEntity;