I need to map values to a std::string ( with the following map, and BOOST_FUSION_ADAPT_STRUCT )
std::map< TYPEX, std::string> author2name;
struct Emp
{
std::string name;
TYPEX author;
};
With the following code i want to generate my output:
karma::rule< it, std::string()> quote = '"' >> karma::string >> '"';
karma::rule< it, Emp> emp = karma::delimit('\t')[ quite << quite[ author2name[ karma::_1] ]];
Emp x;
karma::generate( std::ostream_iterator<char>(std::cout), emp, x);
But it doesn't compile.
And is there a way that i could write a header like this:
karma::rule< it, std::vector<std::string>()> header = karma::delimit('\t')[ % quote];
karma::rule< it, Emp> emp = header >> karma::eol >> karma::delimit('\t')[ quite << quite[ author2name[ karma::_1] ]];
karma::generate( std::ostream_iterator<char>(std::cout), {"A", "B", "C"},emp, x);
There is a number of small paper-cuts that killed you there :)
Working example:
Live On Coliru
Prints:
The things that caused trouble:
boost::spirit::ostream_iterator
andkarma::format
for more user-friendly APIAdd the missing parentheses on
emp
:Here:
you index [] into a
std::map
using ...qi::_1
. That can't compile. What you wanted was to invoke the Phoenix lazy expression template ofoperator[]
. You have to include the Phoenix header and forceauthor2name
to be a Phoenix reference actor:Also, to have an auto-rule in the presence of Semantic Actions, you need to assign the rule using
%=
(otherwise Karma will suppress all automatic attribute propagation)