精神 X3 无法传播可选<vector>类型的属性

spirit x3 cannot propagate attributes of type optional<vector>

本文关键字:gt vector 类型 属性 X3 lt 传播 精神      更新时间:2023-10-16

一个简单的解析器,就像在Coliru上一样。解析器-(+x3::alpha)应该能够像 Qi 一样传播类型 boost::optional<std::string> 的属性。但它不编译。

std::string const input = "abc";
boost::optional<std::string> attr;
if(x3::parse(boost::begin(input),boost::end(input),
    -(+x3::alpha),
    attr)) {
    std::cout<<"match!"<<std::endl;
}
else {
    std::cout<<"NOT match!"<<std::endl;
}

我不认为规范性声明"应该能够 [...]就像齐"砍柴一样。X3不是Qi的进化,原因非常好(比如这个(。

一个经常重复出现的模式是在更复杂的传播方案中需要类型提示。丑陋的冗长方式可能是这样的:

    -(x3::rule<struct _, std::string> {} = +x3::alpha),

住在科里鲁

或者你可以使用我之前描述的技巧:

namespace {
    template <typename T>
    struct as_type {
        template <typename Expr>
            auto operator[](Expr&& expr) const {
                return x3::rule<struct _, T>{"as"} = x3::as_parser(std::forward<Expr>(expr));
            }
    };
    template <typename T> static const as_type<T> as = {};
}

住在科里鲁