Brackets in template parameters in Boost Spirit rules and grammars

Looking at this example to implement the Spirit parser, something caught me when I tried to write something like that.

The grammar attribute template parameter ( std::map<std::string, std::string>() ) and the rule signature template parameter (for example, qi::rule<Iterator, std::string()> key, value ) contain round brackets.

 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 had never seen this before, and I inadvertently missed it when I was writing my own version. This leads to the fact that my parser does not generate the correct output execution time (the output map is empty).

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

+4
source share
1 answer
 std::map<std::string, std::string>() 

This is a type of function.

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

Without () type is simply " std::map<std::string, std::string> ", which is incorrect.

+6
source

All Articles