Matthias Buelow wrote:
Jeff Schwab wrote:
but provides excellent support for arbitrarily high-level
abstractions.
^^^^^^^^^^^^^^^^^^^^^^
Please elaborate.
C++ is a language that relies heavily on, and provides
correspondingly great support for, libraries. No matter how tricky
code gets, it can almost always be safely encapsulated behind a
clean, easy-to-use interface. When you first starting writing C++
code, you can use goodies like the standard string class and the
standard container types right away, with very little understanding
of how they work. As your own code becomes more complicated, you
can provide easy access to it through simple, intuitive APIs,
without hurting performance. Even the standard library is written
in plain old C++ code, without any voodoo you couldn't use in your
own code.
Complicated ideas can be represented directly in C++ code through
the static type system. The ideas are then combined using a
relatively small set of syntactic constructs that have been
assigned particular meanings, either by convention or formal
standards. Once you understand the syntax and the conventions, you
can mix and match all kinds of different ideas from different
people, and -- this is the amazing part -- the compiler can help
determine whether particular ways of connecting ideas actually make
sense. That's not to say you don't have to think; on the contrary,
writing good C++ code often requires more thought than writing a
"good enough" program in (say) Python. The C++ compiler, though,
can help you in ways no other compiler can, or at least none I've
ever seen. For example:
http://www.boost.org/libs/concept_check/concept_check.htm
I think the proof, for many of us, was the STL. Before I had seen
STL containers, I struggled briefly with MFC. If I wanted to store
objects in an MFC container, I had to inherit them from a
particular class, override virtual methods, and generally jump
through a bunch of artificial hoops.
The developers of the STL took a different approach: They wrote
containers that could hold objects of any type supporting particular
syntax and semantics. The syntax they chose was the syntax
supported by the primitive types inherited from C. Container
elements had to be assignable, copyable, constructible without
arguments, etc. Most of my object types already supported those
concepts, using the expected syntax, so they just worked with the
STL containers, right out of the box. Then I found out that the
same algorithm, literally a single piece of source code, could work
with any container type that supported a particular set of
concepts. Most of the standard library containers don't even
accept containers, but instead take iterators, which are in turn
classified according to the sorts of ideas they represent, all
using the same old pointer-style syntax inherited from C. Then I
found out that I had been wasting my time writing my own string
class, because the one in the standard library supported more
functionality with better syntax: Array-style indexed access to
characters, concatenation using operator+, random access
iterators... all the other things I liked about plain char*,
without the headaches.
The same approach continues to be useful; in fact, some of the
neater items commonly used in modern C++ are smart pointers, which
support the same old syntax as raw pointers, but actually
encapsulate potentially complicated ideas. Dereferencing a smart
pointer can, for example, automatically obtain and release a Mutex
lock. Boost Shared Pointers (or std::tr1::shared_ptr), using the
same syntax, support automatic, reference-counted garbage
collection.
http://www.boost.org/libs/smart_ptr/shared_ptr.htm
The last time I had to deal directly with garbage collection in C
was to extend Tcl, and I remember staring at the screen, walking
through the code, trying to convince myself that I had called the
right increment and decrement functions, on the right structures,
at exactly the right places... Man, I don't *ever* want to go back
there.
But that's all just the beginning. Moving forward, it turns out
you can actually implement a significant portion of most programs'
functionality long before you get any run-time data, and thereby
get a tremendous amount of help from the compiler during
development. Getting the old syntax to support these new
"meta-programming" techniques can make for cryptic code, but
because of C++'s incredible support for encapsulation, the
functionality is still easily accessed from client code through
libraries:
http://en.wikipedia.org/wiki/Template_meta-programming
http://www.boost.org/libs/mpl/doc/index.html
Even features not "natively" supported by C++ can usually be
implemented by easy-to-use libraries. Sure, reference-counted
smart pointers can be used for simple garbage collection, but do
you want to see something really cool? Here's a library I haven't
started using in production code yet, but I'm itching to try:
http://www.boost.org/doc/html/lambda.html
This library supports the kinds of expressions I usually use in
scripting languages, but with the all the compile-time goodness I've
come to expect from C++: static concept checks, verification that
function argument types match their declarations, etc. Python
doesn't do any of that for me. I still have to write just as many
unit tests for the C++ version as for the Python version, but the
tests don't fail nearly as often, and all kinds of design issues
are caught earlier with C++ than with Python. I like Ruby a lot
because the code is aesthetically beautiful, but when I'm actually
writing code to get something done, I stick with C++.
C++ is a complicated language, but the wonder of it is that you can
get started with less training than it takes to write decent C
code. The more you learn, the more C++ rewards you. I remember
someone I used to work with, who had a morbid fear of C++, taking
one look at a typical C++ reference book and laughing derisively
(yes, derisively, just like an arrogant Bond villain). "How do
they expect anybody to learn all that?" he asked. The answer is
that you don't have to learn it all before you can use it. When
somebody says they "know" C++, I always wonder what they mean. It's
like a ship's captain saying he "knows" the ocean. C++ is my
Desert Island Language, and if you invest some time in it, I can
almost guarantee that you'll be glad you did.
Wow, a true hallelujah moment. And so right.