How can I resolve circular dependency between variable templates in C ++?

I am engaged in programming templates in C ++, trying to implement some kind of parser library for educational purposes, using only templates.

example of using my library:

std::stringstream ss { "identif1er 123123 hell0 world 3rror" };

// zM = zero or more , aO = any of

using Identifier = Matcher<Alpha, zM<AlphaNum>>;
using Number = Matcher<Digit, zM<Digit>>; 

Matcher<aO<Identifier, Number>> numberOrIdentifier;

while(!ss.eof()) {
    if(ss.peek() == ' ') ss.ignore(1);

    if(numberLetters.s_match(ss)) {
        std::cout << "Token: " << (*numberLetters.val) << std::endl;
    } else {
        std::cout << "Error\n";
    }
}

this works, but I ran into a problem that is sometimes bad for defining a pair of types, which depends on another and vice versa.

I just want to know how I can do this, for example:

using Expr = Matcher<..... Factor ....>
using Factor = Matcher<aO<Number, Expr>>;

How can I make a forward declaration of a factor using only constructor types and arguments? is it possible ?, note: im, using an empty "I" structure to mark the recursion.

Link to template definition:

pattern template

+4

Source: https://habr.com/ru/post/1658970/


All Articles