ARTICLE AD BOX
There's two major issues-
1. Comparing a std::variant<lookAheadToken, Token> directly against a lookAheadToken
2. return null; In C++, NULL is literally just the integer 0. You are telling the compiler to return 0 for a function that expects a std::variant
You can use anenum class to clearly express the result-
enum class PrecedenceResult { Token1, Token2, Equal }; template <typename T, typename U = T> PrecedenceResult comparePrecedence(T token_1, U token_2) { Precedence p1 = getPrecedence(token_1.value); Precedence p2 = getPrecedence(token_2.value); if (p2 > p1) return PrecedenceResult::Token2; if (p2 == p1) return PrecedenceResult::Equal; return PrecedenceResult::Token1; }Make the while condition more readable-
while (parserUtils::isBinary(lookAhead.value) && parserUtils::comparePrecedence(lookAhead, op) == PrecedenceResult::Token2)Hope this also helps!
You are comparing a std::variant with a concrete type, which is the root cause of the error.
Your function returns:
std::variant<T, U>So this expression:
parserUtils::comparePrecedence<lookAheadToken, Token>(lookAhead, op) == lookAheadattempts to evaluate:
std::variant<lookAheadToken, Token> == lookAheadTokenThere is no operator== defined between std::variant and lookAheadToken, hence the error:
no operator "==" matches these operands
std::variant does not implicitly compare itself to one of its alternatives. Even if the underlying value matches, you must explicitly extract it.
Also, your function contains:
return NULL;which is invalid for std::variant and will lead to further issues (it cannot represent NULL unless one of the alternatives is a pointer type or you explicitly use std::monostate).
You must extract the value from the variant before comparing:
auto result = parserUtils::comparePrecedence<lookAheadToken, Token>(lookAhead, op); if (std::holds_alternative<lookAheadToken>(result) && std::get<lookAheadToken>(result) == lookAhead) { // ... }Explore related questions
See similar questions with these tags.
