<分区>
我正在编写一个解析器,我正在尝试插入一个迭代器作为模板。
当我写 template<typedef class Iterator = std::string::iterator>
代码按预期编译。我想我应该可以删除默认值,所以我想写 template<typedef class Iterator>
.我认为这应该没问题,因为我稍后专门研究了模板。然而,这不会编译错误:Error (active) type name is not allowed
.
这是怎么回事?为什么会出现此错误?
这里是示例代码:
#include <string>
#include <boost\spirit\include\qi.hpp>
namespace qi = boost::spirit::qi;
template<typedef class Iterator> //<-- This does not compile
//template<typedef class Iterator = std::string::iterator> //<-- This would compile
class Rules_template
{
private:
typedef qi::rule<Iterator> skipper_rule;
typedef qi::rule<Iterator> line_rule;
typedef qi::rule<Iterator, std::string(), skipper_rule> string_rule;
typedef qi::rule<Iterator, float, skipper_rule> float_rule;
line_rule endofline = qi::lit("\r\n") | qi::lit("\n\r") | qi::lit('\n');
skipper_rule skip = qi::lit(' ') | qi::lit('\t') | qi::lit('\f') | qi::lit('\v') | (qi::lit('\\') >> endofline);
string_rule comment = qi::lexeme['#' >> *(qi::char_ - qi::char_('\n') - qi::char_('\r')) >> endofline];
public:
string_rule & Comment() { return comment; }
skipper_rule & Skip() { return skip; }
};
static Rules_template<std::string::iterator> Rules_str;
void CHECK(bool b)
{
if (b)
std::cout << "check ok" << std::endl;
else
std::cout << "check not ok" << std::endl;
}
int main(int argc, char ** argv)
{
std::string test = "#This is a comment\n";
std::string expect = "This is a comment";
std::string actual;
auto it = test.begin();
CHECK(true == qi::phrase_parse(it, test.end(), Rules_str.Comment(), Rules_str.Skip(), actual));
CHECK(it == test.end());
CHECK(expect.compare(actual) == 0);
}
编辑 1:
这可能是特定于编译器的。我在使用 VS2015 时遇到此错误。