括号中的模板参数在升压精神的规则和语法 [英] Parentheses in template parameters in Boost Spirit rules and grammars

查看:150
本文介绍了括号中的模板参数在升压精神的规则和语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看着<一个href=\"http://boost-spirit.com/home/articles/qi-example/parsing-a-list-of-key-value-pairs-using-spirit-qi/\"相对=nofollow>这个例子实施精神解析器的,东西抓住了我,当我试图写类似的东西。

语法的属性模板参数(的std ::地图&LT;的std ::字符串,性病::字符串&GT;())和规则的签名模板参数(例如:齐::规则&LT;迭代器,标准::字符串()&GT;键,值)。包含括号

 命名空间补气=的boost ::精神::补气;模板&LT; typename的迭代器&GT;
结构keys_and_values
  :补气::语法&LT;迭代器,性病::地图&LT;的std ::字符串,性病::字符串&GT;()&GT; //&LT; - 在这里括号
{
    keys_and_values​​()
      :keys_and_values​​ :: base_type(查询)
    {
        查询=对&GT;&GT; *((齐亮::(';')|'和;')&GT;&gt;对);
        对关键=&GT;&GT; - ('='&GT;&GT;值);
        关键=补气::字符_(A-ZA-Z_)&GT;&GT; *补气::字符_(A-ZA-Z_0-9);
        值= +补气::字符_(A-ZA-Z_0-9);
    }
    齐::规则&LT;迭代器,性病::地图&LT;的std ::字符串,性病::字符串&GT;()&GT;查询; //&LT; - 在这里括号
    齐::规则&LT;迭代器,性病::对&LT;的std ::字符串,性病::字符串&GT;()&GT;对; //&LT; - 在这里括号
    齐::规则&LT;迭代器,标准::字符串()&GT;键,值; //&LT; - 在这里括号
};

我从来没有见过这一点,写我自己的版本时,我无意间忽略然后。这导致我的解析器不产生正确的输出运行时间(输出映射为空)。

什么是这些括号的目的是什么?我的猜测是,这使得这条规则的那个类型的对象的属性。


解决方案

 的std ::地图&LT;的std ::字符串,性病::字符串&GT;()

这是一个函数类型。

()使得这意味着返回函数的的std ::地图&LT;的std ::字符串,性病::字符串&GT; 并没有参数。

如果没有(),类型仅仅是的std ::地图&LT;的std ::字符串,性病::字符串&GT; ,这是不正确的。

Looking at this example for implementing a Spirit parser, something caught me out when I was trying to write something similar.

The attribute template parameter of the grammar (std::map<std::string, std::string>()) and the signature template parameter of the rules (e.g. qi::rule<Iterator, std::string()> key, value) contain parentheses.

namespace qi = boost::spirit::qi;

template <typename Iterator>
struct keys_and_values
  : qi::grammar<Iterator, std::map<std::string, std::string>()> // <- parentheses here
{
    keys_and_values()
      : keys_and_values::base_type(query)
    {
        query =  pair >> *((qi::lit(';') | '&') >> pair);
        pair  =  key >> -('=' >> value);
        key   =  qi::char_("a-zA-Z_") >> *qi::char_("a-zA-Z_0-9");
        value = +qi::char_("a-zA-Z_0-9");
    }
    qi::rule<Iterator, std::map<std::string, std::string>()> query; // <- parentheses here
    qi::rule<Iterator, std::pair<std::string, std::string>()> pair; // <- parentheses here
    qi::rule<Iterator, std::string()> key, value; // <- parentheses here
};

I have never seen this before, and I unintentionally omitted then when writing my own version. This lead to my parser not generating the correct output run time (the output map is empty).

What is the purpose of these parentheses? My guess is that this makes the attribute of this rule an object of that type.

解决方案

std::map<std::string, std::string>()

This is a function type.

The () makes this mean "a function that returns a std::map<std::string, std::string> and has no parameters."

Without the (), the type is just "std::map<std::string, std::string>," which is not correct.

这篇关于括号中的模板参数在升压精神的规则和语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆