Boost :: spirit ::业力:重复不会在重复或克莱恩之星中起作用吗?

时间:2013-04-08 12:06:58

标签: c++ boost boost-spirit boost-spirit-karma

这是一段非常简单的代码,它使用boost::spirit::karma以graphviz点语言生成格式化输出:

#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星中根本无效。我也尝试了repeat[]指令,但这也行不通。

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

1 个答案:

答案 0 :(得分: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;
}