Boost Spirit规则和语法中的模板参数中的括号

Chr*_*lin 4 c++ templates boost boost-spirit boost-spirit-qi

看看这个用于实现Spirit解析器的示例,当我尝试编写类似的东西时,有些事情让我感到困惑.

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

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
};
Run Code Online (Sandbox Code Playgroud)

我之前从未见过这个,在编写自己的版本时我无意中省略了.这导致我的解析器没有生成正确的输出运行时间(输出映射为空).

这些括号的目的是什么?我的猜测是,这使得该规则的属性成为该类型的对象.

Jam*_*lis 6

std::map<std::string, std::string>()
Run Code Online (Sandbox Code Playgroud)

这是一种功能类型.

()让这意味着"一个返回的功能std::map<std::string, std::string>,并没有参数."

没有(),类型只是" std::map<std::string, std::string>,"这是不正确的.

  • 哈!什么都没有特别提升/精神相关! (2认同)