为什么 boost::spirit::qi::parse() 没有设置这个 boost::variant 的值?

2024-04-21

当尝试将文本解析为 boost::variant 时,变体的值不会更改。 解析器本身似乎工作正常,所以我的假设是我对变体代码做了错误的事情。

我使用的是 boost 1.46.1,以下代码在 Visual Studio 2008 中编译。

第一次更新

hkaiser 指出,规则和语法模板参数不能是Variant but Variant().
这有点“进一步”,因为我现在有一个编译错误boost_1_46_1\boost\variant\variant.hpp(1304)。评论说:

// NOTE TO USER :
// Compile error here indicates that the given type is not 
// unambiguously convertible to one of the variant's types
// (or that no conversion exists).

显然表达式的属性(qi::double_ | +qi::char_) is not boost::variant<double, std::string>。但那又是什么呢?

第二次更新

Using typedef boost::variant<double, std::vector<char>> Variant;适用于解析器。然而,这并不像 std::string 那么容易使用......

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

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

    typedef std::string::const_iterator Iterator;

    const std::string a("foo"), b("0.5");

    // This works
    {
        std::string stringResult;
        Iterator itA = a.begin();
        const bool isStringParsed =
            qi::parse(itA, a.end(), +qi::char_, stringResult);

        double doubleResult = -1;
        Iterator itB = b.begin();
        const bool isDoubleParsed =
            qi::parse(itB, b.end(), qi::double_, doubleResult);

        std::cout
                << "A Parsed? " << isStringParsed <<
                ", Value? " << stringResult << "\n"
                << "B Parsed? " << isDoubleParsed <<
                ", Value? " << doubleResult << std::endl;

        // Output:
        // A Parsed? 1, Value? foo
        // B Parsed? 1, Value? 0.5
    }


    // This also works now
    {
        typedef boost::variant<double, std::vector<char>> Variant; // vector<char>, not string!

        struct variant_grammar : qi::grammar<Iterator, Variant()> // "Variant()", not "Variant"!
        {
            qi::rule<Iterator, Variant()> m_rule; // "Variant()", not "Variant"!

            variant_grammar() : variant_grammar::base_type(m_rule)
            {
                m_rule %= (qi::double_ | +qi::char_);
            }
        };

        variant_grammar varGrammar;

        Variant varA(-1), varB(-1);
        Iterator itA = a.begin();
        const bool isVarAParsed = qi::parse(itA, a.end(), varGrammar, varA);
        Iterator itB = b.begin();
        const bool isVarBParsed = qi::parse(itB, b.end(), varGrammar, varB);

        // std::vector<char> cannot be put into std::cout but
        // needs to be converted to a std::string (or char*) first.
        // The conversion I came up with is very ugly but it's not the point
        // of this question anyway, so I omitted it.
        // You'll have to believe me here, when I'm saying it works..

        // Output:
        // A (variant): Parsed? 1, Value? foo, Remaining text = ''
        // B (variant): Parsed? 1, Value? 0.5, Remaining text = ''
    }

    return 0;
}

规则的属性必须使用函数声明语法指定:

qi::rule<Iterator, Variant()> m_rule;

我没有尝试过,但我相信在这个改变之后它会起作用(顺便说一句,语法也需要同样的)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 boost::spirit::qi::parse() 没有设置这个 boost::variant 的值? 的相关文章

随机推荐