生成的字符串,如果布尔属性为真(因缘对口补气::匹配) [英] Generate string if boolean attribute is true (karma counterpart to qi::matches)

查看:174
本文介绍了生成的字符串,如果布尔属性为真(因缘对口补气::匹配)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,我们要来分析和处理Boost.Spirit生成简单的C ++成员函数的声明。

Imagine we want to parse and generate simple C++ member function declarations with Boost.Spirit.

齐语法可能是这样的:

function_ %= type_ > id_ > "()" > matches["const"];

这意味着,该功能是否常量保存在布尔

如何使用噶编写相应的发电机?

How to write the corresponding generator with Karma?

function_ %= type_ << ' ' << id_ << "()" << XXX[" const"];

在这里,我们要消耗一个布尔属性的指令,执行嵌入的发电机,如果属性为真正和什么也不做,否则。我们想要的东西,做下面的测试取得成功。

Here, we want a directive that consumes a boolean attribute, executes the embedded generator if the attribute is true and does nothing otherwise. We want something that makes the following tests succeed.

test_generator_attr("abc", XXX["abc"], true);
test_generator_attr("", XXX["abc"], false);

就是这样一个指令已经可以在Boost.Spirit?

Is such a directive already available in Boost.Spirit?

推荐答案

这在此刻进入我的脑海里的第一件事是

The first thing that enters my mind at the moment is

bool const_qualifier = true;

std::cout << karma::format(
      karma::omit[ karma::bool_(true) ] << " const" | "",
      const_qualifier);

这感觉有点...笨拙。我要看看后来我我忘了:)

It feels a bit... clumsy. I'll have a look later what I'm forgetting :)

更新下面是使用人缘::符号℃的稍微更优雅起飞;&GT;

#include <boost/spirit/include/karma.hpp>

namespace karma = boost::spirit::karma;

int main()
{
    karma::symbols<bool, const char*> const_;
    const_.add(true, "const")(false, "");

    for (bool const_qualifier : { true, false })
    {   
        std::cout << karma::format_delimited("void foo()" << const_, ' ', const_qualifier) << "\n";
    }   
}

打印:

void foo() const 
void foo()  

这篇关于生成的字符串,如果布尔属性为真(因缘对口补气::匹配)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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