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

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

问题描述

查看这个例子用于实现一个Spirit解析器,当我试图写一些类似的东西时,有些东西吸引了我。

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

属性模板参数语法( std :: map< std :: string,std :: string>())和规则的签名模板参数(例如 qi :: rule< Iterator,std :: string()> key,value )包含括号。

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>()

函数类型。

()意味着返回 std :: map< std :: string,std :: string> 并且没有参数。

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

(),类型只是 std :: map< std :: string,std :: string> ,这是不正确的。

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

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

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