c++ - boost spirit 业力多种可选

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

我看到一个错误,但没有看到解决方案。一、相关代码:

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_ 规则相匹配,但我不明白为什么?我错过了什么?

最佳答案

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

    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_)];
}

关于c++ - boost spirit 业力多种可选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15437282/

相关文章:

c++ - 如何避免#include 对外部库的依赖

c++ - using 声明是否仅导入在 using 声明之上声明的重载?

c++ - 在 OS X 上使用 icu 构建 boost.locale

c++ - 使用 boost::interprocess::shared_ptr 共享生命周期跨进程

c++ - Boost.Spirit将表达式转换为AST

c++ - Boost.Spirit.Qi : dynamically create "difference" parser at parse time

c++ - 如何编译peercoin来生成Windows二进制文件?

c# - CreateCompatibleDC(IntPtr.Zero) 返回 IntPtr.Zero

c++ - boost spirit 莱克斯和气。集成跳过解析器

c++ - Boost Spirit语法eol