-1
我正在写一个解析器,我试图插入一个迭代器作为模板。 当我编写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);
}
EDIT 1:
这可能是编译器具体。 VS2015出现这个错误。当我更改模板声明
typedef declaration invalid in parameter declaration
:
template<class Iterator>
代码编译(0失误,0
'模板'肯定是在标准C语法错误++。你确定这是原始代码吗? –
cpplearner
你的意思是使用'typename'? –
看到这里的例子http://en.cppreference.com/w/cpp/language/template_parameters – user3159253