using enum declaration

2 is a big number in programming. When something repeat more than one time in the code, I tend to pack them in a function, member function or lambda, depend of the context.

using enum declaration added by C++20 and allows us to avoid qualifying enum type when reaching enumerators. With it, instead of:

Expr Parser::comparison() {
Expr expr = term();

while (match({TokenKind::GREATER, TokenKind::GREATER_EQUAL, TokenKind::LESS, TokenKind::LESS_EQUAL})) {
Token op = previous();
Expr right = term();
expr = BinaryExpr(expr, op, right);
}
return expr;
}

we can write:

Expr Parser::comparison() {
Expr expr = term();

using enum TokenKind;
while (match({GREATER, GREATER_EQUAL, LESS, LESS_EQUAL})) {
Token op = previous();
Expr right = term();
expr = BinaryExpr(expr, op, right);
}
return expr;
}

Thanks for reading :)

more on: