1

我编写了下面列出的代码。编译器向我报告一个错误:'3 个重载都不能转换所有参数类型'。

我使用 MSVC 11.0 和 Boost 1.51.0。表达式的每个分支都m_oQueryIterationExpression可以正常工作,但它们不能一起工作。有什么线索吗?

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

namespace Ns
{
    struct Regexp     { std::string m_strEntity; };
    struct String     { std::string m_strEntity; };
    struct Identifier { std::string m_strEntity; };

    typedef int Number;

    typedef std::string Operation;
    typedef boost::variant<Regexp, Number, String, Identifier> Operand;
    typedef boost::tuple<Operation, boost::optional<std::vector<Operand> > > OperationWithOperands;

    struct QueryConcatenation;

    typedef boost::tuple<boost::recursive_wrapper<QueryConcatenation>, boost::optional<char> > typeA;
    typedef std::vector<std::vector<OperationWithOperands> > typeB;

    typedef boost::variant<typeA, typeB> QueryIteration;

    struct QueryConcatenation {
        typedef std::vector<QueryIteration> TEntity;
        TEntity m_oEntity;
    };
}

int main()
{
    using namespace Ns;
    namespace qi = boost::spirit::qi;

    qi::rule<char*, QueryConcatenation()>                                m_oQueryConcatenationExpression;
    qi::rule<char*, QueryIteration()>                                    m_oQueryIterationExpression;
    qi::rule<char*, std::vector<std::vector<OperationWithOperands> >() > m_oQueryNode;

    m_oQueryIterationExpression %= 
        qi::attr_cast<typeA, typeA>( '(' >> m_oQueryConcatenationExpression >> ')' >> -(qi::char_("*+?")) )
        | m_oQueryNode;
}
4

1 回答 1

3

好吧,问题是,您的分配模棱两可的,编译器只是让您知道。走着瞧吧:

typedef boost::tuple<
            boost::recursive_wrapper<STreeConstructionRuleQueryConcatenation>, 
            boost::optional<char>
        > typeA;
typedef std::vector<std::vector<STreeConstructionRuleOperationWithOperands> > typeB;

typedef boost::variant<typeA, typeB> typeAB;
typedef typeAB STreeConstructionRuleQueryIteration;

现在让我们看看一些规则中的属性兼容性:

qi::rule<Iterator, typeA(),  ascii::space_type> ruleA;
qi::rule<Iterator, typeB(),  ascii::space_type> ruleB;
qi::rule<Iterator, typeAB(), ascii::space_type> ruleAB;

// these are intended to compile, and they do:
ruleA  %= ( '(' >> m_oQueryConcatenationExpression >> ')' >> -(qi::char_("*+?")) );
ruleB  %= m_oQueryNode;
// so far, so good

// ---- Now, ideally we like the assignments to be mutually exclusive:

// This fails to assign, good! :
// ruleB  %= ( '(' >> m_oQueryConcatenationExpression >> ')' >> -(qi::char_("*+?")) );

// But HERE is the surprise:
ruleA  %= m_oQueryNode; // uhoh m_oQueryNode can be assigned to a typeA attribute as well

这就是在解析器表达式的第二个分支上使变体的初始化模棱两可的原因:它可能是任何一个!

幸运的是,有一个非常简单的解决方案:只需明确类型即可:

qi::rule<Iterator, typeAB(), ascii::space_type> ruleAB;
ruleAB %= ruleA | ruleB;

您会看到,通过使子规则公开变体的显式类型成员,您消除了所有疑问(完全匹配对编译器来说很好)。可以达到相同的效果:

ruleAB %= qi::attr_cast_type<typeA, typeA>( '(' >> m_oQueryConcatenationExpression >> ')' >> -(qi::char_("*+?")) ) | ruleB;

这消除了对另一个子规则的需要,但以易读性为代价,IMO。

这是您的完整原始代码,用子规则修复:http: //liveworkspace.org/code/5a7a8046b713beefba211a6a54219368(更改命名...)

于 2012-10-18T23:49:46.533 回答