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

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

   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;


   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, kalb at, bjorn.karlsson at, gregod at, wekempf at