C++ spirit x3无法传播可选类型的属性<;向量>;

C++ spirit x3无法传播可选类型的属性<;向量>;,c++,boost,boost-spirit,boost-spirit-qi,boost-spirit-x3,C++,Boost,Boost Spirit,Boost Spirit Qi,Boost Spirit X3,一个简单的解析器。解析器-(+x3::alpha)应该能够像Qi一样传播类型为boost::optional的属性。但它不编译 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!

一个简单的解析器。解析器
-(+x3::alpha)
应该能够像Qi一样传播类型为
boost::optional
的属性。但它不编译

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;
}
std::string const input=“abc”;
boost::可选属性;
if(x3::parse(boost::begin(输入),boost::end(输入),
-(+x3::α),
attr){

std::cout我不认为规范性的说法“应该像气那样[…]能够”切割木头。X3不是气的进化,有很好的理由(比如这样)

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

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

或者您可以使用hack

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 = {};
}
名称空间{
模板
结构类型{
模板
自动运算符[](Expr&&Expr)常量{
返回x3::rule{“as”}=x3::as_解析器(std::forward(expr));
}
};
模板静态常量as_type as={};
}

很好的解决方案!但我仍然想知道x3不支持如此明显的复合属性传播的原因是什么?它不明显。它涉及两个重要的转换。容器类型总是很棘手(
-(a%b)
对于
向量
-(a*)
自然等同于
a*
。在Qi中,“足够明显”的原因可能是子解析器内部过早地将其属性保存在显式合成类型中,导致在复制属性时效率低下