@@ -706,6 +706,61 @@ This allows for the original element to be modified independently of
706706the expression instance and to also allow the expression to be
707707evaluated using the current value of the element.
708708
709+ Note: Any variable reference provided to a given symbol_table
710+ instance, must have a life time at least as long as the life-time of
711+ the symbol_table instance. In the event the variable reference is
712+ invalidated before the symbol_table or any dependent expression
713+ instances have been destructed, then any associated expression
714+ evaluations or variable referencing via the symbol_table instance will
715+ result in undefined behaviour.
716+
717+ The following bit of code instantiates a symbol_table and expression
718+ instance, then proceeds to demonstrate various ways in which
719+ references to variables can be added to the symbol_table, and how
720+ those references are subsequently invalidated resulting in various
721+ forms of undefined behaviour.
722+
723+ typedef exprtk::symbol_table<double> symbol_table_t;
724+
725+ symbol_table_t symbol_table;
726+ expression_t expression;
727+
728+ {
729+ double x = 123.4567;
730+ symbol_table.add_variable("x", x);
731+ } // Reference to variable x has been invalidated
732+
733+ std::deque<double> y {1.1, 2.2, 3.3};
734+
735+ symbol_table.add_variable("y", y.back());
736+
737+ y.pop_back(); // Reference to variable y has been invalidated
738+
739+ std::vector<double> z {4.4, 5.5, 6.6};
740+
741+ symbol_table.add_variable("z", z.front());
742+
743+ z.erase(z.begin());
744+ // Reference to variable z has been invalidated
745+
746+ double* w = new double(123.456);
747+
748+ symbol_table.add_variable("w", *w);
749+
750+ delete w; // Reference to variable w has been invalidated
751+
752+ const std::string expression_str = "x + y / z * w";
753+
754+ // Compilation of expression will succeed
755+ parser.compile(expression_str,expression);
756+
757+ expression.value();
758+ // Evaluation will result in undefined behaviour
759+
760+ symbol_table.get_variable("x")->ref() = 135.791;
761+ // Assignment will result in undefined behaviour
762+
763+
709764The example below demonstrates the relationship between variables,
710765symbol_table and expression. Note the variables are modified as they
711766normally would in a program, and when the expression is evaluated the
0 commit comments