Output of a boost::variant type using boost::spirit::karma

左心房为你撑大大i 提交于 2019-12-24 13:19:46

问题


I'm trying to output parameters, they can either be a single parameter or a vector of parameters. The following code does not what I'd like it to do:

#include <iostream>
#include <string>
#include <boost/variant.hpp>
#include <boost/spirit/include/karma.hpp>
namespace karma = boost::spirit::karma;

typedef std::vector<int> ParameterList;
typedef boost::variant<int, ParameterList> Parameter;

main()
{
    using karma::int_;
    using karma::eol;
    using karma::lit;

    std::string generated;
    std::back_insert_iterator<std::string> sink(generated);

    karma::rule<std::back_insert_iterator<std::string>, ParameterList()> parameterListRule = int_ % lit(", ");
    karma::rule<std::back_insert_iterator<std::string>, Parameter()> parameterRule = (int_ | parameterListRule) << eol;

    karma::generate(sink, parameterRule, 1); // Works
    karma::generate(sink, parameterRule, ParameterList{1, 2}); // Doesn't work
    std::cout << generated << "\n";
}

(The real code has other types of parameters, not just int, and I cannot just make everything a ParameterList.)

I do not quite understand how to make this work, in particular since an analogous construction for parsing works fine for me using boost::spirit::qi.


回答1:


In your second call you didn't put a variant in the generator, but the rule expects one.

#include <iostream>
#include <string>
#include <boost/variant.hpp>
#include <boost/spirit/include/karma.hpp>
namespace karma = boost::spirit::karma;

typedef std::vector<int> ParameterList;
typedef boost::variant<int, ParameterList> Parameter;

main()
{
    using karma::int_;
    using karma::eol;
    using karma::lit;

    std::string generated;
    std::back_insert_iterator<std::string> sink(generated);

    karma::rule<std::back_insert_iterator<std::string>, ParameterList()> parameterListRule = int_ % lit(", ");
    karma::rule<std::back_insert_iterator<std::string>, Parameter()> parameterRule = (int_ | parameterListRule) << eol;

    karma::generate(sink, parameterRule, 1); // Works
    // use variant here:
    Parameter test(ParameterList{1, 2});
    karma::generate(sink, parameterRule, test);
    std::cout << generated << "\n";
}


来源:https://stackoverflow.com/questions/18088818/output-of-a-boostvariant-type-using-boostspiritkarma

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