Boost Karma:使用boost :: optional <boost :: variant <...>&gt;

时间:2018-02-08 16:37:36

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

我正在尝试在Karma生成器的boost::variant内使用boost::optional。我已经能够解决这个问题了:

using FooVariant = boost::variant<int>;
using FooOptional = boost::optional<FooVariant>;

template<typename OutputIt = boost::spirit::ostream_iterator>
struct FooGenerator
    : boost::spirit::karma::grammar<OutputIt, FooOptional()>
{
    FooGenerator()
        : FooGenerator::base_type(start_)
    {
        namespace bsk = boost::spirit::karma;
        start_ = '[' << ( bsk::int_ | '*' ) << ']';
    }

    boost::spirit::karma::rule<OutputIt, FooOptional()> start_;
};

int main()
{
    FooOptional fop1;
    std::cout << boost::spirit::karma::format(FooGenerator<>(), fop1) << std::endl;

    FooOptional fop2 = FooVariant{123};
    std::cout << boost::spirit::karma::format(FooGenerator<>(), fop2) << std::endl;
}

这里的输出是:

[*]
[*]

我希望的是什么。

通过将FooVariant更改为:using FooVariant = int;,我更改的第一件事就是或多或少的完整性检查。这输出:

[*]
[123]

我想看到的!所以在我的第一个代码中,变体只有一种类型,所以我尝试添加第二种类型,只是为了看:

using FooVariant = boost::variant<int, double>;
...
start_ = '[' << ( ( bsk::int_ | bsk::double_ ) | '*' ) << ']';

但接下来我们又回到了:

[*]
[*]

然后我尝试专门为变体添加规则:

using FooVariant = boost::variant<int, double>;
using FooOptional = boost::optional<FooVariant>;

template<typename OutputIt = boost::spirit::ostream_iterator>
struct FooGenerator
    : boost::spirit::karma::grammar<OutputIt, FooOptional()>
{
    FooGenerator()
        : FooGenerator::base_type(start_)
    {
        namespace bsk = boost::spirit::karma;
        foovar_ = (bsk::int_ | bsk::double_);
        start_ = '[' << ( foovar_ | '*' ) << ']';
    }

    boost::spirit::karma::rule<OutputIt, FooVariant()> foovar_;
    boost::spirit::karma::rule<OutputIt, FooOptional()> start_;
};

int main()
{
    FooOptional fop1;
    std::cout << boost::spirit::karma::format(FooGenerator<>(), fop1) << std::endl;

    FooOptional fop2 = FooVariant{123};
    std::cout << boost::spirit::karma::format(FooGenerator<>(), fop2) << std::endl;
}

这给出了编译错误:

alternative_function.hpp:127:34: error: no member named 'is_compatible' in
      'boost::spirit::traits::compute_compatible_component<boost::variant<int, double>, boost::optional<boost::variant<int, double> >, boost::spirit::karma::domain>'
            if (!component_type::is_compatible(spirit::traits::which(attr_)))
                 ~~~~~~~~~~~~~~~~^

看起来模板生成正在尝试确保boost::variantboost::optional兼容,但对我而言,问题是&#34;为什么要尝试确保他们&#39;完全兼容?&#34;

我该如何做到这一点?

1 个答案:

答案 0 :(得分:4)

我在另一个答案中告诉你我将如何处理这个问题:Boost Karma: generate default text when boost::optional is unset

这不仅避免了这个问题,还简化了AST /数据类型。

现在,因为你强迫我¹,我挖了进来。问题来自于foovar_ | '|'是一个 alternative-expression ,并且它以某种方式确认该属性必须兼容。在那次检查期间,假设

  • 如果a具有属性attr(a)b具有属性attr(b),那么
  • a | b应该有variant<attr(a), attr(b)

它确实进行了一些逻辑检查(比如何时attr(a) == attr(b)),但没有进行逻辑检查,如果attr(b)unused_type那么它也应该与optional<attr(a)>兼容,而不是只需variant<attr(a), unused_type>甚至variant<attr(a)>

现在,为什么我不感兴趣呢?所以,这就是强制事情的方式。

    foovar_ = bsk::int_ | bsk::double_;
    fooopt_ = (bsk::eps(is_initialized_(bsk::_val)) | '*') << -foovar_;
    start_ = '[' << fooopt_ << ']';

是将其编码为 alternative-expression 以外的其他内容。在这里,我们只是信任-foovar_来说些什么。另外,我们也做了

bsk::eps(is_initialized_(bsk::_val)) | '*'

含义:如果变量已初始化,则全部,否则生成'*'

现在你并不严格需要第三条规则,如果你喜欢只写代码,你可以写:

    start_ = '[' << (bsk::eps(is_initialized_(bsk::_val)) | '*') << -foovar_ << ']';

样本

哦,我差点忘了:

struct is_initialized_f {
    template<typename T>
    bool operator()(boost::optional<T> const& opt) const { return opt.is_initialized(); }
};
boost::phoenix::function<is_initialized_f> is_initialized_;

实现帮助程序Phoenix actor检查初始化状态。

<强> Live On Coliru

#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix.hpp>
using FooVariant = boost::variant<int, double>;
using FooOptional = boost::optional<FooVariant>;

template<typename OutputIt = boost::spirit::ostream_iterator>
struct FooGenerator
    : boost::spirit::karma::grammar<OutputIt, FooOptional()>
{
    FooGenerator()
        : FooGenerator::base_type(start_)
    {
        namespace bsk = boost::spirit::karma;
        namespace phx = boost::phoenix;

        foovar_ = bsk::int_ | bsk::double_;
        //fooopt_ = (bsk::eps(is_initialized_(bsk::_val)) | '*') << -foovar_;
        start_ = '[' << (bsk::eps(is_initialized_(bsk::_val)) | '*') << -foovar_ << ']';
    }

  private:
    struct is_initialized_f {
        template<typename T>
        bool operator()(boost::optional<T> const& opt) const { return opt.is_initialized(); }
    };
    boost::phoenix::function<is_initialized_f> is_initialized_;
    boost::spirit::karma::rule<OutputIt, FooVariant()> foovar_;
  //boost::spirit::karma::rule<OutputIt, FooOptional()> fooopt_;
    boost::spirit::karma::rule<OutputIt, FooOptional()> start_;
};

int main()
{
    for (FooOptional fop : { FooOptional{}, {FooVariant{123}}, {FooVariant{3.14}} }) {
        if (std::cout << boost::spirit::karma::format(FooGenerator<>(), fop))
            std::cout << "\n";
        else
        {
            std::cout.clear();
            std::cout << "#Error\n";
        }
    }
}

打印

[*]
[123]
[3.14]

¹笑话

²我们会得到与“有人忘记”或“这是回归”押韵的非权威性答案