
On Mon, Aug 23, 2010 at 10:05 AM, Mathias Gaunard <mathias.gaunard@ens-lyon.org> wrote:
On 23/08/10 13:43, Dmitry Goncharov wrote:
What's wrong with the following? Aside from the know limitations. (local::f cannot be a template arg or be a function template). void f() { struct local { static void f() {} }; local::f(); }
Does your library have advantages over this?
I believe his macro has mechanisms for catching some variables in scope (at least, ScopeExit had).
Something you would do with
struct local { local(int j_) : j(j_) {}
int operator()(int i) { return i + j; }
private: int j; }; local f(j);
could be written as something like
LOCAL_FUNCTION_BEGIN(f, (j), (int i)) { return i + j; } LOCAL_FUNCTION_END
Surely you would agree the second form is quite more succint
That is correct: The advantage of Boost.LocalFunction over simply using a static member of a local class is that Boost.LocalFunction supports binding of variables in scope (similarly to what Boost.ScopeExit does). In addition, note how the binding of `j` in the example above does not require you to specify its type (the example above just assumes `j` is `int` but Boost.LocalFunction, as well as Boost.ScopeExit, can automatically determine the type using typeof-like features). That is important because you might want to simply say "I am using the variable in scope `j` in my local function" without worrying about specifying its type. This type might change later and if it were specified explicitly also in the local function declaration it would require to change the code in multiple places making maintainance harder. Finally, note that the binding can be done by value (in which case the local function will use a copy of the binded variable with its value at the point of the local function declaration), or by reference (in which case the local function will use the current binded variable value at the point of the local function invocation), or by const value, or by const reference. -- Lorenzo