I've been fiddling with some boost spirit stuff, and my code seems to start infinitely looping somewhere in the standard library stuff.

Any ideas what's wrong?

The qi::phrase_parse works very well, but the karma gets me.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
//#include <boost/spirit.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
using namespace std;

int main()
	using namespace boost::spirit;
	string input = "100  ,     200    , 200 , 300 , 300, 300, 400, 400, 400, 400";
	vector<int> r;
	//int value = 0;
	//boost::spirit::qi::parse(input.begin(),input.end(),boost::spirit::qi::int_, value);
	//cout << value << endl;
	qi::phrase_parse( input.begin(), input.end(), *(qi::int_), (*(qi::space) >> ',' >> *(qi::space)), r);
	string out;
	back_insert_iterator<string> inserter(out);
	karma::generate_delimited( inserter, '(' << karma::int_ << ')', (*(karma::space) << ',' << *(karma::space)), r);
	cout << out << endl;
	//for_each(r.begin(), r.end(), [] (int n) { std::cout << n << std::endl; } );

In the middle of this document:

I'm not an expert with boost.spirit, but I don't think that it makes much sense to specify a delimiter which has "any number of spaces" as in the delimiter generator expression (*(karma::space) << ',' << *(karma::space)) . This is likely to be the explanation for the infinite loop (probably the star (or "any number of") isn't meant to ever be used in a generator expression, but compiles nonetheless and causes an infinite loop if used as a generator). So, I would replace it with either (karma::space << ',') or (',' << karma::space) (or with two spaces).

From the examples on the boost.spirit webpage it also seems that the generator expression given to the "generate_delimited" function should be one that has an "any number of" type format over the attribute to be outputted. In other words, it seems that the expected behaviour of your code would be to output only the first number in the sequence, and that this generator would be worth a try in case it is so:

karma::generate_delimited( inserter, '(' << karma::int_ << ')' << *('(' << karma::int_ << ')'), (karma::space << ',' << karma::space), r);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.