1

这是一段非常简单的代码,用于boost::spirit::karma在 graphviz dot 语言中生成格式化输出:

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

int main() {
    vector<int> v{0,1,2,3};
    unsigned count = 17;
    {
        namespace karma = boost::spirit::karma;
        karma::generate(ostream_iterator<char>(cout), *(karma::duplicate['c' << karma::int_ << '_' << karma::lit(count) << "[xlabel=" << karma::int_ << "];\n"]), v);
    }

    return 0;
}

我希望这会产生:

c0_17[xlabel=0];
c1_17[xlabel=1];
c2_17[xlabel=2];
c3_17[xlabel=3];

相反,它产生了:

c0_17[xlabel=1];
c2_17[xlabel=3];

这意味着该duplicate[]指令在 kleene star 中根本无效。我也尝试过该repeat[]指令,但这也不起作用。

我究竟做错了什么?我正在使用提升版本 1.53.0。我用g++4.7 和clang++3.2 编译了代码,都产生了相同的结果。

4

1 回答 1

0
#include <iostream>
#include <iterator>
#include <vector>
#include <boost/spirit/include/karma.hpp>
using namespace std;

int main() {
    vector<int> v{0,1,2,3};
    unsigned count = 17;
    {
        namespace karma = boost::spirit::karma;
        //karma::rule<ostream_iterator<char>, int()> xlabel = karma::duplicate['c' << karma::int_ << '_' << karma::lit(count) << "[xlabel=" << karma::int_ << "];\n"];
        //karma::generate(ostream_iterator<char>(cout), *xlabel, v);
        karma::generate(ostream_iterator<char>(cout), *karma::attr_cast<int>(karma::duplicate['c' << karma::int_ << '_' << karma::lit(count) << "[xlabel=" << karma::int_ << "];\n"]), v);
    }

    return 0;
}
于 2013-04-08T13:35:17.210 回答