【问题标题】:Boost Spirit Karma multiple optionalsBoost Spirit Karma 多种选择
【发布时间】:2013-03-15 16:18:24
【问题描述】:

我看到了一个我没有解决的错误。一、相关代码:

namespace C {

    struct RangeEntry {
        size_t byte;
        boost::optional<size_t> bit;
    };

    struct Range {
        RangeEntry firstPart;
        boost::optional<RangeEntry> secondPart;
        boost::optional<size_t> shift;
    };
}

BOOST_FUSION_ADAPT_STRUCT(
    C::RangeEntry,
    (size_t, byte)
    (boost::optional<size_t>, bit)
)

BOOST_FUSION_ADAPT_STRUCT(
    C::Range,
    (C::RangeEntry , firstPart)
    (boost::optional<C::RangeEntry> , secondPart)
    (boost::optional<size_t> , shift)
)

... Declare the rules ...

karma::rule<Iterator, C::Range()> range;
karma::rule<Iterator, C::RangeEntry()> range_part;

... Define rules ...

range_part %= no_delimit[ulong_ << -(lit(":") << ulong_)];
range %= no_delimit[range_part << -(lit("-") << range_part)] << -(lit("<<") << ulong_);

range %= 部分,我得到编译错误

/usr/include/boost/spirit/home/karma/numeric/detail/numeric_utils.hpp:504:30: error:
invalid operands to binary expression 
('C::RangeEntry' and 'C::RangeEntry')
    return floor(num / spirit::traits::pow10<T>(exp));
                 ~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

我猜它正在尝试将 RangeEntry 与 ulong_ 规则匹配,但我不知道为什么?我错过了什么?

【问题讨论】:

  • 只是一个hint
  • 问题中缺少 RangeEntry 使其几乎无法回答。下次,请确保包含 SSCCE!
  • @llonesmiz 您可能有兴趣知道现在有 Coliru,它允许您通过 c++filt 管道输出,以便更轻松地进行拆解:coliru.stacked-crooked.com/…
  • @sehe 这真的很有趣,谢谢。
  • @sehe 对不起,我忘了包括(我没有理由不能这样做)。不过,您的“幻想”示例几乎是正确的。 (我自己的无符号长结构和可选,而不是一对)。

标签: c++ boost boost-spirit boost-spirit-karma boost-optional


【解决方案1】:

no_delimit 指令正在重新组合您暴露的融合序列。请注意,以下内容会编译:

    range %= range_part << -(lit("-") << range_part) << -(lit("<<") << ulong_);

甚至

    range %= no_delimit[range_part << -(lit("-") << range_part) << -(lit("<<") << ulong_)];

AFAICT 规则的定义没有分隔符,所以 no_delimit 在这里无论如何应该是多余的。


我“幻想”了一个 RangeEntry 类型,只是为了让它成为一个独立的样本:

#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/karma.hpp>

namespace karma = boost::spirit::karma;

namespace C {
    typedef std::pair<unsigned long, boost::optional<unsigned long> > RangeEntry;

    struct Range {
        RangeEntry firstPart;
        boost::optional<RangeEntry> secondPart;
        boost::optional<size_t> shift;
    };
}

BOOST_FUSION_ADAPT_STRUCT(
    C::Range,
    (C::RangeEntry , firstPart)
    (boost::optional<C::RangeEntry> , secondPart)
    (boost::optional<size_t> , shift)
    );

//... Declare the rules ...

int main()
{
    typedef char* Iterator;
    karma::rule<Iterator, C::Range()> range;
    karma::rule<Iterator, C::RangeEntry()> range_part;

    //... Define rules ...

    using namespace karma;
    range_part %= no_delimit[ulong_ << -(lit(":") << ulong_)];
    range %= no_delimit[range_part << -(lit("-") << range_part) << -(lit("<<") << ulong_)];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多