Boost logo

Boost :

From: Andrey Semashev (andysem_at_[hidden])
Date: 2007-08-24 12:59:45


Hello Ilya,

Friday, August 24, 2007, 3:10:57 PM, you wrote:

> Andrey Semashev wrote:

>> Consider the following examples:
>>
>> Example 1:
>>
>> void foo(std::set< int >& my_set)
>> {
>> bool commit = false;
>> std::set< int >::iterator it = my_set.insert(10);
>> BOOST_SCOPE_EXIT((my_set)(it)(commit))
>> {
>> // Which one will be erased here?
>> // Not necessarily the one we intended to.

> Sorry for such late response.
> Can you explain what is intended value of it?

The scope exit block is supposed to undo the insertion into the
my_set. So the intended value is the value returned from "insert".

>> Example 2:
>>
>> class A
>> {
>> int m_i;
>>
>> void foo()
>> {
>> m_i = 1;
>> BOOST_SCOPE_EXIT((m_i))
>> {
>> // What's the value of m_i here?
>> std::cout << m_i << std::endl;
>> }
>> BOOST_SCOPE_EXIT_END
>>
>> bar();
>> }

> I believe BOOST_SCOPE_EXIT should be moved to the end of foo(). I am
> thinking about BOOST_SCOPE_EXIT as of try/finally with different layout
> of braces:

No, you missed the point. The bar function may throw, and the scope
exit block is there just for that reason - this block should be
executed regardless from the way of returning from foo.

As for "finally" analogy, while BOOST_SCOPE_EXIT does the same thing,
it has no such drawback like the need to be written somewhere in the
end of the function. It may be declared right below the code it is
intended to complement. This is noted in the library docs.

-- 
Best regards,
 Andrey                            mailto:andysem_at_[hidden]

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk