Output of a boost::variant type using boost::spiri

2019-08-03 23:49发布

问题:

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