簡體   English   中英

為什么 boost::spirit::qi::parse() 沒有設置這個 boost::variant 的值?

[英]Why does boost::spirit::qi::parse() not set this boost::variant's value?

當試圖將文本解析為 boost::variant 時,變量的值不會改變。 解析器本身似乎工作正常,所以我的假設是我對變體代碼做錯了。

我正在使用 boost 1.46.1, 以下代碼在 Visual Studio 2008 中編譯

第一次更新

hkaiser 指出,規則和語法模板 arguments 不能是Variant而是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_)的屬性不是boost::variant<double, std::string> 但那是什么?

第二次更新

使用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;
}

必須使用 function 聲明語法指定規則的屬性:

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

我沒有嘗試過,但我相信它會在此更改后起作用(順便說一句,語法也是如此)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM