Generate string if boolean attribute is true (karma counterpart to qi::matches)

好久不见. 提交于 2019-12-05 19:32:36

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 :)

UPDATE Here's a slightly more elegant take using karma::symbols<>:

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

Prints:

void foo() const 
void foo()  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!