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

2024-04-20

看着这个例子 http://boost-spirit.com/home/articles/qi-example/parsing-a-list-of-key-value-pairs-using-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
};

我以前从未见过这一点,并且在编写自己的版本时无意中忽略了这一点。这导致我的解析器无法生成正确的输出运行时间(输出映射为空)。

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


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

这是一个函数类型。

The ()使得这意味着“一个返回a的函数std::map<std::string, std::string>并且没有参数。”

如果没有(),类型只是“std::map<std::string, std::string>,”这是不正确的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Boost Spirit 规则和语法中模板参数中的括号 的相关文章

随机推荐