Re: Terminology - RAII vs. Scope Guard vs...?
Gareth Owen <gwowen@gmail.com> wrote:
You could define
* a language that forces copyable object to have a java-like
user-provided clone() for all objects, and no implicit copying or
assignment for user defined types (just as many C++ polymorphic
classes do)
* or a language where every object acted like std::shared_ptr
* or even (god forbid) a language where every object acted like
std::auto_ptr
and still have RAII, just as long as you had destruction when the last
reference vanished.
I suppose you could, but then you would be seriously hindering the
flexibility and efficiency of the language. For instance, you could not
anylonger create generic code, eg. generic data containers, that would
work on basic types as well as objects, each object would be slow to
create and destroy and would consume extra memory, it would be complicated
to make one object have the same state as another (ie. assignment), it
would be difficult to implement a copy-on-write mechanism, and so on.
Moreover, the copying/assignment mechanism essential to RAII would
still be there, it's just that it would be limited to the native "smart
pointer" type and you would be unable to create your own special smart
pointers (because of the inability of treating objects by value and
implementing copy constructors and copy assignment operators for them).
(And even moreover, forcing each object to be handled by a reference
counting smart pointer can more easily lead to situations where the
object is never freed or is freed too soon. It's basically impossible
for a compiler to automatically detect such situations.)