public inbox for archer@sourceware.org
 help / color / mirror / Atom feed
From: Jim Blandy <jimb@red-bean.com>
To: Tom Tromey <tromey@redhat.com>
Cc: Project Archer <archer@sourceware.org>
Subject: Re: C++ draft
Date: Mon, 26 Sep 2011 21:01:00 -0000	[thread overview]
Message-ID: <CAHgb+0kzjTsn3VU1K0KWzeED9PY-f78Apq5-PypT=Og7nEftRA@mail.gmail.com> (raw)
In-Reply-To: <BANLkTi=r43A6tKmHnK1sF3ya80xBPU+a9edzdN09mGUk8vGf+A@mail.gmail.com>

I came across this thread, and thought I might bring up Mozilla's
experience converting our JavaScript engine to C++. (It seems like
this discussion hasn't moved over to the gdb mailing list, so I'm
sending this here.)

C++'s richer type system is wonderful. We try to get strict typing
everywhere we can, and it has helped a lot. Legibility is way up since
I started at Mozilla, in 2008, just before we switched.

Contrary to your plan, we did *not* use C++ exceptions, but stuck with
our primitive "false/NULL return value means error; please clean up
and propagate" system (we carry details about the error in a structure
off to the side). It's actually a lot of work to transition C code to
being robust in the presence of exceptions. Pretty much every place
you call malloc/realloc needs to use some kind of automatic clean-up
facility like std::auto_ptr, unless you're positive nobody will ever
add a call to a function that might throw an exception before that
malloc'd storage is freed, or linked into whatever data structure it's
destined for, or the like. The essential problem is that the old C
code, by explicitly propagating the errors, effectively makes a big,
visible distinction between calls that might throw, and calls that
won't. All the control flow is explicit. When you switch to
exceptions, that distinction goes away: any call could potentially
leave the scope, and every pointer that's live across that call must
be prepared for the possibility.

We do use RAII a lot, though; that works great, and is a big win, even
when the code propagates errors explicitly. Constructors and
destructors are extremely helpful for all sorts of things.

One thing that surprised me is that we have a lot of little classes
that are only used for local variables, never allocated on the heap.
We use them to abstract out common patterns of code that occur *within
functions*. Because inlining is trustworthy in G++, the generated code
doesn't change much, but it's much more legible.

  reply	other threads:[~2011-09-26 21:01 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-27 21:08 Tom Tromey
2011-06-27 21:23 ` [Archer] " Joel Brobecker
2011-06-29 19:55   ` Tom Tromey
2011-06-29 21:47     ` [Archer] " Joel Brobecker
2011-06-28  4:33 ` Matt Rice
2011-06-28  8:21 ` Yao Qi
2011-06-28 12:21   ` Gary Benson
2011-06-28 17:08     ` Matt Rice
2011-06-28 17:36     ` Jan Kratochvil
2011-06-29 20:08     ` Tom Tromey
2011-06-29 20:06   ` Tom Tromey
2011-06-30  6:06     ` Yao Qi
2011-09-26 21:01       ` Jim Blandy [this message]
2011-06-29 18:52 ` Stan Shebs
2011-06-29 19:03   ` [Archer] " Joel Brobecker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAHgb+0kzjTsn3VU1K0KWzeED9PY-f78Apq5-PypT=Og7nEftRA@mail.gmail.com' \
    --to=jimb@red-bean.com \
    --cc=archer@sourceware.org \
    --cc=tromey@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).