Search code examples
c++templatesboost-spirit-qi

Type Name is Not Allowed


I am writing a parser, and I am trying to insert an iterator as a template. When I write template<typedef class Iterator = std::string::iterator> the code compiles as expected. I think I should be able to remove the default, so I want to write template<typedef class Iterator>. I figure this should be ok since I specialize the template later. However this does not compile with the error: Error (active) type name is not allowed.

What is going on here? Why am I getting this error?

Here the sample code:

#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:

This may be compiler specific. I am getting this error with VS2015.


Solution

  • When I compile this code using Code::Blocks compiler, I get this error:

    typedef declaration invalid in parameter declaration
    

    When I change the template declaration to:

    template<class Iterator>
    

    code compiles (0 errors, 0 warnings) and is able to execute.

    Same result when template declaration is changed to:

    template<class Iterator = std::string::iterator>
    

    code compiles (0 errors, 0 warnings) and is able to execute.

    Execution in either case yields the following output:

    check ok
    check ok
    check ok