Simulating the Elisp dynamic scoping mechanism in C++
Abstract
To add dynamic scoping to C++, the C++ equivalent of Elisp's
"unwind-protect" and Java's "finally" mechanism needs to be used. In
the absence of exceptions this is very simple to code:
int global;
void foo()
{
int global_temp = global;
global = new_value;
/* rest of foo method */
global = global_temp;
}
In the case that exceptions are used then destructors must be used,
using a design pattern that C++ creator
Bjarne Stroustrup calls
Resource
Acquisition Is Initialization (or R.A.I.I. for short). An
alternative approach to this one is to prefer to use C++
preprocessor macro calls to ordinary function/method calls. Here is
an example where the F3 and F4 macro functions can access
the variable x from the function f1.