提升业力语义动作调用图 [英] boost karma semantic action call map
问题描述
我需要将值映射到std :: string(具有以下映射,以及BOOST_FUSION_ADAPT_STRUCT)
I need to map values to a std::string ( with the following map, and BOOST_FUSION_ADAPT_STRUCT )
std::map< TYPEX, std::string> author2name;
struct Emp
{
std::string name;
TYPEX author;
};
使用以下代码,我想生成我的输出:
With the following code i want to generate my output:
karma::rule< it, std::string()> quote = '"' >> karma::string >> '"';
karma::rule< it, Emp> emp = karma::delimit('\t')[ quite << quite[ author2name[ karma::_1] ]];
Emp x;
karma::generate( std::ostream_iterator<char>(std::cout), emp, x);
但是它不能编译.
有没有一种方法可以编写这样的标题:
And is there a way that i could write a header like this:
karma::rule< it, std::vector<std::string>()> header = karma::delimit('\t')[ % quote];
karma::rule< it, Emp> emp = header >> karma::eol >> karma::delimit('\t')[ quite << quite[ author2name[ karma::_1] ]];
karma::generate( std::ostream_iterator<char>(std::cout), {"A", "B", "C"},emp, x);
推荐答案
有许多小剪纸在那杀了你:)
There is a number of small paper-cuts that killed you there :)
工作示例:
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <map>
namespace karma = boost::spirit::karma;
namespace phx = boost::phoenix;
enum TYPEX { AUTHOR1, AUTHOR2, AUTHOR3, AUTHOR4 };
std::map<TYPEX, std::string> author2name;
struct Emp {
std::string name;
TYPEX author;
};
BOOST_FUSION_ADAPT_STRUCT(Emp, name, author) // boost 1_59
//BOOST_FUSION_ADAPT_STRUCT(Emp, (std::string, name)(std::string, author)) // older boost
int main() {
using it = boost::spirit::ostream_iterator;
karma::rule<it, std::string()> quote;
karma::rule<it, TYPEX()> author;
karma::rule<it, Emp()> emp;
{
using namespace karma;
quote %= '"' << string << '"';
author = quote [ _1 = phx::ref(author2name)[ _val ] ];
emp %= delimit('\t')[ quote << author ];
}
Emp x { "one", AUTHOR2 };
author2name[AUTHOR2] = "TWO!";
std::cout << karma::format(emp, x);
}
打印:
"one" "TWO!"
引起麻烦的事情
- 建议使用
boost::spirit::ostream_iterator
和karma::format
以获得更用户友好的API -
在
emp
上添加缺少的括号:
- suggest to use
boost::spirit::ostream_iterator
andkarma::format
for more user-friendly API Add the missing parentheses on
emp
:
karma::rule<it, Emp()> emp;
注意:最近的提升(1_59 IIRC)不再要求.这就是为什么我只在Coliru上找到
NOTE: very recent boost (1_59 IIRC) doesn't not require these anymore. Which is why I found out only on Coliru
在这里:
Here:
quote[ author2name[ karma::_1] ]
使用... qi::_1
将[]索引到std::map
中.那不能编译.您想要的是调用operator[]
的Phoenix 惰性表达式模板.您必须包括Phoenix标头,并强制author2name
成为Phoenix参考角色:
you index [] into a std::map
using ... qi::_1
. That can't compile. What you wanted was to invoke the Phoenix lazy expression template of operator[]
. You have to include the Phoenix header and force author2name
to be a Phoenix reference actor:
quote [ _1 = phx::ref(author2name)[_1] ]
还请注意,分配回
_1
很重要!
此外,要在存在语义动作的情况下具有自动规则,您需要使用%=
分配规则(否则Karma将禁止所有自动属性传播)
Also, to have an auto-rule in the presence of Semantic Actions, you need to assign the rule using %=
(otherwise Karma will suppress all automatic attribute propagation)
这篇关于提升业力语义动作调用图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!