
On 8/20/07, Steven Watanabe <steven@providere-consulting.com> wrote:
AMDG
Giovanni Piero Deretta <gpderetta <at> gmail.com> writes:
The problem is that ScopeExit relies on local structs which can't be used in templates.
If you look at Implementation section ( http://tinyurl.com/2gerqy ) you'll notice params_1_t and scope_exit_body_t local structures.
Though, I see how it can be avoided: (void*) new params_1_t(...).
The local structs are not essential. You can use tuples.
Well, you need local structures because you need to put the body of the closure somewhere. I'm attaching an experiment I have done a while ago. It is far from perfect, as it use gcc native typeof (could easily use BOOST_TYPEOF though), closure arity is limited to three arguments and you need to specify at least one closed-over variable. Also, you can't use commas in the closure body because I'm embedding it in a preprocessor sequence. Someone with more preprocessor-fu than me could easily overcome these limits though. The biggest limitation of this approach is that the generated function object is monomorphic (that is its argument types are fixed). BTW, it is little more than a curiosity, but by using gcc compound statements, true unnamed lambdas can be simulated quite well. HTH, gpd