c++ - Boost::spirit::karma:复制在 repeat 或 kleene star 中不起作用?

标签 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 star 中根本无效。我也尝试过 repeat[] 指令,但效果不佳。

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

最佳答案

#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;
}

关于c++ - Boost::spirit::karma:复制在 repeat 或 kleene star 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15878643/

相关文章:

c++ - 如何将鼠标移动转换为相机平移

c++ - 如何将 asio 与设备文件一起使用

c++ - 如何使用 boost::spirit 解析数学表达式并将其绑定(bind)到函数

c++ - <boost/lexcal_cast.hpp> 在哪里?

c++ - 如何使用 Boost.Spirit.Qi 增量解析(并作用于)大文件?

c++ - 如何使用 Boost.Spirit 在 C++ 中序列化一个 double,以便输出接近标准流的输出?

c++ - 递归目录和文件流以及搜索字符串

c++ - 实现标记 union 的移动构造函数

c++ - 使用 boost::lexical_cast 将 std::string 的 std::array 转换为相同大小的 std::tuple

c++ - Boost::spirit 属性类型不崩溃