From: Ronald Landheer-Cieslak on
ManicQin wrote:
> Thank you guys,
> About the first example I gave, it wasn't an example for RAII , I just
> tried to explain what do I mean in re-scoping.
>
> But take Ronald's example: ScopedLocks.
>
> If I un-intentionally delete the scopes ("{}") I can harm the logical
> flow of the system, suddenly the mutex applies to the entire scope of
> the function and not only for the resources I tried to protect.
>
> If you take auto_ptr and ScopedLock you can see a difference between
> them (besides their purpose of course!)
> for example with auto_ptr (IMO) the block of code that it resides in
> only determines the life span of the auto_ptr (and by that the pointer
> that he holds).
> in ScopedLock the block of code that it resides in determines the
> block of code that it operates on.
>
> I just think that the "closing curly brackets" were given a new power
> and the question is: "does a small curly bracket is strong enough tool
> for the mission?"
If the people working with your code understand RAII, they'll be on the
look-out for the curly bracket anyway. If they don't, they should at
least read the comment that comes with the curly bracket.

> On a personal note,
> In the company that I am employed we use the "C++ is a better C"
> paradigm (we use classes!),
> from time to time I'm trying to introduce people to new concepts and
> usually I hit the brick wall of
> "And what if the next guy wont know what you did and how to work with
> it?" (which is a very reasonable argument)
> So each time I try to introduce a new concept I check to see how
> destructive is it and does it worth it.
I know what it's like to work in such an environment - it tends to dumb
down the code (for reasonable reasons, but still). If what you're afraid
of is that your colleagues (or the new guy) will miss the curly bracket
or miss its point, be sure to put the appropriate comments in. The
comment should explain what's going on (not just say "scope ends here",
but rather "this is where such-and-such gets unlocked, which is
necessary because ...") so if they take the trouble reading the comment,
they'll know what's going on and either read up on their C++ or leave
the code as-is.

As Ulrich said, RAII is an essential technique in C++ - among other
things, it's why we don't need "finally" clauses and why there really is
no reason to ever have a resource leak in a program written in C++.
IIRC, even "C with classes" had it - which is really what the "C++ is a
better C" mantra results in IMHO: using C with classes rather than C++.
It you can't use RAII because some-one else might not understand,
perhaps you should prepare a presentation on RAII (or hire some-one to
do a presentation to the team) - definitely worth the effort!

Ronald

--- news://freenews.netfront.net/ - complaints: news(a)netfront.net ---

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]