OSNews article about C++09 degenerates into C++ vs. D discussion

Steve Horne stephenwantshornenospam100 at aol.com
Thu Nov 23 16:24:04 PST 2006


On Tue, 21 Nov 2006 09:01:58 +0000 (UTC), Boris Kolar
<boris.kolar at globera.com> wrote:

>== Quote from Walter Bright (newshound at digitalmars.com)'s article
>> No. But most RAII usage is for managing memory, and Boris didn't say why
>> he needed RAII for the stack allocated objects.
>
>Mostly for closing OS handles, locking, caching and stuff. Like:
>  getFile("a.txt").getText()
>
>Normally, one would have to:
>  1. open the file (which may allocate caching buffers, lock the file, etc.)
>  2. use the file (efficiently)
>  3. close the file (frees handles, buffers, releases locks, etc.)
>
>It's a common design pattern, really.

I do a lot the same too. And then I find that there are cases where I
really wish I could switch off the constructor and destructor, since I
want uninitialised memory ready to hold an object that I can't
construct yet.

In C++ I fix that by using placement new and explicit destructor
calls, and using types along the lines of char[sizeof(blah)].

In D it works out a bit different. I use a struct instead of a class,
and so I opt out of having initialisation and cleanup by default. If I
want them, I can use the scope statement to ensure they get done.

Of course that also means opting out of having inheritance etc.

Anyway, beyond a certain point, you cannot eliminate complexity. You
can only move it from one place to another.

In the case stack-allocated objects, there is (currently) a bit more
complexity in D. You can't really do RAII except in the slightly
defeating-the-point approach of explicit initialisation and cleanup
(though the scope statement at least allows you to keep them together
in the code). It can be a bit annoying, I agree. But then, D is being
actively developed and stack allocation of objects does seem to be on
the to-do list.

-- 
Remove 'wants' and 'nospam' from e-mail.



More information about the Digitalmars-d mailing list