2

Help me please find a solution to a seeming simple problem. I need generate a string from a container in a form index=value.

For example:

seq = { 10, 20, 30 } output = "1=10&2=20&3=30"

But I found no way to do so with the boost::spirit::karma without writing a custom gererator. For some reasons it's impossible to use a semantic action with karma::lit. Maybe I missed something?

void index(std::string &string_)
{
    static size_t index_ = 1;
    string_ = boost::lexical_cast < std::string > (index_++);
}

int main()
{
    using boost::spirit::karma::uint_;
    using boost::spirit::karma::generate;
    using boost::spirit::karma::lit;

    std::string string_;

    boost::array < unsigned int, 4 > array_ = { 1, 2, 3, 5 };

    generate(
            std::back_insert_iterator < std::string > (string_), 
            (lit("")[&index] << "=" << uint_) % '&',
            array_);

    return 0;
}

At compilation I get the following error:

cannot convert parameter 1 from 'boost::spirit::karma::transform_attribute<boost::spirit::unused_type,boost::spirit::unused_type>::type' to 'std::basic_string<_Elem,_Traits,_Ax>
4

1 回答 1

1

解决了

int main()
{
using boost::spirit::karma::uint_;
using boost::spirit::karma::generate;
using boost::spirit::karma::lit;

using boost::phoenix::val;
using boost::phoenix::ref;

std::string string_;

boost::array < unsigned int, 4 > array_ = { 1, 2, 3, 5 };

unsigned int count_ = 1;

generate(
    std::back_insert_iterator < std::string > (string_), 
    (lit(val(ref(count_)++)) << "=" << uint_) % '&',
    array_);

return 0;
}
于 2012-03-02T04:14:57.843 回答