
Matt Gruenke wrote:
First, a usage example:
glob_t result_glob;
BOOST_FINALLY__BEGIN { globfree( &result_glob ); } BOOST_FINALLY__END
BEGIN/END variant of BOOST_SCOPE_EXIT exits but I like it less because POSSIBLY SMALL code IS SURROUNDED by capital letters: BOOST_SCOPE_EXIT_BEGIN( (result_glob) ) { globfree( &result_glob ); } BOOST_SCOPE_EXIT_END; It's strange that your code compiles because $ tail -n 8 finally.cpp int main() { int a, b; BOOST_FINALLY__BEGIN { a + b; } BOOST_FINALLY__END } $ g++ finally.cpp finally.cpp: In destructor `main()::boost_finally_25::~boost_finally_25()': finally.cpp:26: error: use of `auto' variable from containing function finally.cpp:24: error: `int a' declared here finally.cpp:26: error: use of `auto' variable from containing function finally.cpp:24: error: `int b' declared here So, you can use only global or static variables inside the block.
The basic idea is to use the destructor of an instance of a local struct as the mechanism through which the "at scope exit" code gets invoked. In order to facilitate multiple such blocks in the same scope, type and variable names are made unique by appending the source file line number to both names.
SCOPE_EXIT is based on the same idea.
I should note that this was designed for use in functions, only. Also, there are some cases in which I've found this unsatisfactory, ...
Can you be more specific about these? -- Alexander Nasonov http://nasonov.blogspot.com Take away love and our earth is a tomb. Robert Browning This quote is generated by: /usr/pkg/bin/curl -L http://tinyurl.com/veusy \ | sed -e 's/^document\.write(.//' -e 's/.);$//' \ -e 's/<[^>]*>//g' -e 's/^More quotes from //' \ | fmt | tee ~/.signature-quote