Boost logo

Boost Users :

Subject: [Boost-users] [spirit::karma | variant] Nested Variants
From: Aaron_Wright_at_[hidden]
Date: 2013-12-11 13:35:52


I am trying to use spirit::karma to generate a CSV file. I want to output
a vector of variants. Unfortunately, I also want to add one more type to
the variant; a string. So I was thinking I could nest variants. I can get
this code to compile, but only the strings print out. What am I doing
wrong?

template< typename Iterator > class Generator :
   public boost::spirit::karma::grammar<
      Iterator,
      std::vector< boost::variant< std::string, boost::variant< bool, int
> > >() >
{
public:

   Generator() : Generator::base_type(start)
   {
      using boost::spirit::karma::string;
      using boost::spirit::karma::bool_;
      using boost::spirit::karma::int_;
      using boost::spirit::karma::eol;

      start = (string | bool_ | int_) % Separator() << eol;
   }

   boost::spirit::karma::rule< Iterator, Csv_generator_input() > start;
};

int main(int argc, char** argv)
{
   std::vector< boost::variant< std::string, boost::variant< bool, int > >
> values;

   values.push_back(std::string("Hello"));
   values.push_back(true);
   values.push_back(false);
   values.push_back(10);
   values.push_back(std::string("World"));

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

   boost::spirit::karma::generate(sink, Generaator(), values); //
generated == "Hello,World", Expected "Hello,true,false,10,World"

   return 0;
}

---
Aaron Wright

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net