I'm creating an expression with exprtk using variables which change constantly.
Do I have to reset and recompile the exprtk::expression using an updated exprtk::symbol_table everytime I change the value of a variable?
Or are the updated values evaluated directly by the existing, compiled expression?
#include <iostream>
#include <string>
#include "exprtk.hpp"
int main() {
std::string expression_string = "y := x + 1";
int x = 1;
exprtk::symbol_table<int> symbol_table;
symbol_table.add_variable("x", x);
exprtk::expression<int> expression;
expression.register_symbol_table(symbol_table);
exprtk::parser<int> parser;
if (!parser.compile(expression_string, expression))
{
std::cout << "Compilation error." << std::endl;
return 1;
}
expression.value(); // 1 + 1
x = 2;
// Do I have to create a new symbol_table, expression and parse again?
// Or does the expression evaluate the new value directly?
expression.value(); // 2 + 1?
return 0;
}
exprtk::expressiondoes not have to be recompiled when the values of the variables referenced byexprtk::symbol_tablechange.expression.value()can be used immediately.According to the documentation (Section 10 - Components), the actual values of the variables referenced in the symbol table are resolved not until the expression is evaluated. So compiling the same expression with the parser has to happen once only.