Re: If GC is the solution, then what is the problem?
charleyb123@gmail.com wrote:
Walter Bright spaketh:
[...]
C++ should have acquired GC about 15 years ago. I think
your comment about too little, too late applies to that as
well. Even if GC were adopted, how many years would it be
before it diffuses through to all the C++ platforms out
there? 10 years?
Gene Bushuyev replied:
Probably never. And the reason that there is no GC neither
in the C++ Standard nor as a typical extension is the lack
of demand. Good C++ practices tend to produce the objects
with automatic storage duration. Resourse management is done
in ctor/dtor, which is robust, flexible and deterministic.
THANK YOU, Gene.
For the false illusion? There IS a large demand for garbage
collection in C++. The standards committee is seriously
considering it for the next version of the standard. Third
party companies are successfully selling garbage collection as
an add in, and an open source implementation is widely used.
None of which, of course, says that we should abandon value
semantics for value oriented types, nor drop constructors and
destructors.
I bite my tongue every time GC comes up. I understand its
utility, as I understand the value of high-level scripting in
langauges like Perl/Python/Ruby. Further, I understand the
value of "higher-level" OO languages with GC and dynamic
typing, like Smalltalk and Lisp.
But, that's not C++ with its compile-time static typing. I
would *never* use GC.
That's an interesting statement, because compile-time static
typing for dynamically allocated objects requires garbage
collection. It can't be done otherwise, or at least, I don't
think so. C++ implementations don't do it; they use the
"undefined behavior" to let them off the hook.
IMHO, for most OO, it's simply a bad way to view the problem.
For the *vast* majority of our designs, object ownership is
defined through well-understood containment within
well-defined (logical) abstractions.
A large majority, yes. And there are certainly objects for
which garbage collection is not really the right solution
(although even with such objects, it permits some form of type
safety, which isn't possible otherwise). I have, in fact, often
wished for a sort of reverse garbage collection as well: rather
than deleting an object when there are no more pointers to it,
it would get rid of all pointers to an object when it the object
is deleted.
The fact that it isn't necessary all of the time doesn't mean
that it isn't useful in enough cases to be worthwhile.
If it's not, IMHO, you are missing key abstractions.
It depends on the object. Many objects DO have explicit
lifetimes. All garbage collection brings to them is an ability
to detect the errors: an explicit and understandable error
message, instead of undefined behavior. Many others however,
don't really need an explicit lifetime, as long as they are
there when needed (i.e. someone holds a pointer to them). For
those, garbage collection is THE solution. (And of course, a
lot of objects have value semantics, and should probably never
be allocated dynamically anyway. For those, of course, garbage
collection is irrelevant.)
If you create an object and you don't know who should "own"
it, you have a primitive design.
I find a lot of cases where the design says that it doesn't
matter.
I admit exceptions to this principle in "strange" domains,
like implementation of an object-database library, but those
tend to be rare. Still, that simply means the "owner" will be
some kind of artifact (not a logical entity, as is most common
in most domains).
All GC does is say, "I create objects but don't feel like
defining an owner, so I just leave my mess for the janitor,
whom I assume will clean up for me in a proper way."
It's not a question of feeling, but of design. There are
objects for which artificially designating an owner is just
extra work, and is, in fact, an artifact of a weakness in the
language.
I'd be *so sad* if the C++ standard got screwed up with GC,
and I had to figure out how to work around it and shut it off.
If you don't like being more productive, that's understandable.
If you're paid by the line, garbage collection will certainly
reduce your income.
--
James Kanze GABI Software
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]