使用boost升压变型::输出::精神::因果报应 [英] Output of a boost::variant type using boost::spirit::karma
本文介绍了使用boost升压变型::输出::精神::因果报应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图输出参数,它们可以是单个参数或参数的向量。下面code不,我想它做什么:
I'm trying to output parameters, they can either be a single parameter or a vector of parameters. The following code does not what I'd like it to do:
#include <iostream>
#include <string>
#include <boost/variant.hpp>
#include <boost/spirit/include/karma.hpp>
namespace karma = boost::spirit::karma;
typedef std::vector<int> ParameterList;
typedef boost::variant<int, ParameterList> Parameter;
main()
{
using karma::int_;
using karma::eol;
using karma::lit;
std::string generated;
std::back_insert_iterator<std::string> sink(generated);
karma::rule<std::back_insert_iterator<std::string>, ParameterList()> parameterListRule = int_ % lit(", ");
karma::rule<std::back_insert_iterator<std::string>, Parameter()> parameterRule = (int_ | parameterListRule) << eol;
karma::generate(sink, parameterRule, 1); // Works
karma::generate(sink, parameterRule, ParameterList{1, 2}); // Doesn't work
std::cout << generated << "\n";
}
(真正的code有其他类型的参数,而不仅仅是诠释,我不能只是让一切参数列表。)
(The real code has other types of parameters, not just int, and I cannot just make everything a ParameterList.)
我不明白如何使这项工作,特别是因为用于分析一个类似的建筑我使用boost ::精神::气为工作正常。
I do not quite understand how to make this work, in particular since an analogous construction for parsing works fine for me using boost::spirit::qi.
推荐答案
在你的第二个电话你没有把一个变种在生成,但规则期望之一。
In your second call you didn't put a variant in the generator, but the rule expects one.
#include <iostream>
#include <string>
#include <boost/variant.hpp>
#include <boost/spirit/include/karma.hpp>
namespace karma = boost::spirit::karma;
typedef std::vector<int> ParameterList;
typedef boost::variant<int, ParameterList> Parameter;
main()
{
using karma::int_;
using karma::eol;
using karma::lit;
std::string generated;
std::back_insert_iterator<std::string> sink(generated);
karma::rule<std::back_insert_iterator<std::string>, ParameterList()> parameterListRule = int_ % lit(", ");
karma::rule<std::back_insert_iterator<std::string>, Parameter()> parameterRule = (int_ | parameterListRule) << eol;
karma::generate(sink, parameterRule, 1); // Works
// use variant here:
Parameter test(ParameterList{1, 2});
karma::generate(sink, parameterRule, test);
std::cout << generated << "\n";
}
这篇关于使用boost升压变型::输出::精神::因果报应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文