1

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.

manlio
  • 18,345
  • 14
  • 76
  • 126
Xoph
  • 459
  • 2
  • 10

1 Answers1

3

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";
}
Mike M
  • 2,263
  • 3
  • 17
  • 31
  • I thought I could simplify my actual problem this far and then screwed it up, introducing this dumb bug. Thank you and sorry, my real problem is now [here](http://stackoverflow.com/questions/18101509/boostspiritkarma-output-of-string-in-quotation-marks). – Xoph Aug 07 '13 at 10:53