spirit x3无法传播类型为optional< vector>的属性. [英] spirit x3 cannot propagate attributes of type optional<vector>

查看:73
本文介绍了spirit x3无法传播类型为optional< vector>的属性.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Coliru 上的简单解析器.解析器-(+x3::alpha)应该能够像Qi那样传播类型为boost::optional<std::string>的属性.但是它不能编译.

A simple parser as on Coliru. The parser -(+x3::alpha) should be able to propagate an attribute of type boost::optional<std::string> as Qi does. But it does not compile.

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

推荐答案

我不认为规范性声明应该像Qi一样能够"砍柴.daccess-ods.un.org daccess-ods.un.org X3不是Qi的演变,出于很好的理由(例如,如此).

I don't think the normative claim "should be able [...] as Qi does" cuts wood. X3 is not an evolution of Qi, for very good reasons (such as this).

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

An oft recurring pattern is that type hints are required in more complicated propagation scenarios. The ugly verbose way could be like this:

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

在Coliru上直播

或者您可以使用黑客 我之前已经描述过 :

Or you can use the hack I described previously:

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 = {};
}

在Coliru上直播

这篇关于spirit x3无法传播类型为optional&lt; vector&gt;的属性.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆