Re: C++ Frequently Questioned Answers
on Tue Nov 06 2007, apm35-AT-student.open.ac.uk wrote:
This lost opportunity to use RAII is probably what David Abrahams
was referring to when he said his code was typically shorter and
simpler in C++.
It was someone else, but I've had the same experience. A combination
of C++ features (mostly operator overloading, templates, and inlining)
make it possible to have abstraction without time and space penalties.
To get equivalently-efficient use of resources in 'C' requires
(sometimes complete) loss of abstraction, resulting in messy,
hard-to-write, hard-to-maintain, incorrect code that sometimes is even
less efficient than the equivalent C++. I can cite many examples, but
here are two:
* High-performance math: expression templates allow optimal
implementations of complex matrix calculations while maintaining
readable syntax. The simplest example is summing an arbitrary
set of matrices (known at compile-time) without creating any
temporary matrices.
Matrix sum = a + b + c ... ;
The optimal code walks the summed matrices in parallel, placing
the sums of each corresponding element into the result.
The equivalent 'C' is bigger, uglier, and even if you manage to
write a library routine that does it just as efficiently for
this case, when you add one more matrix to your sum, you have to
rewrite the library to maintain performance (so naturally you
sacrifice performance). The operations involved in a realistic
case aren't limited to +, and even introducing '-' creates a
combinatorial explosion of possible computations so there's no
way to hardcode a C library that can do the computation
optimally.
* The Python interpreter is written in 'C' and uses
reference-counting as part of its garbage-collection scheme.
Not having the smart pointer abstraction available to manage the
reference counting is a nightmare, and frequently leads to
mistakes that can be hard to detect and debug. Furthermore, all
the manual reference-counting operations account for a great
deal of syntactic overhead. The 'C' code is bigger, uglier, and
more error-prone than the equivalent C++, and no more efficient.
I could go on. Abstraction without efficiency penalty is key to
writing robust, correct, efficient, sustainable software, and 'C'
doesn't provide nearly the capabilities that C++ does in that
department.
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]