Why is not more FreeBSD software written in C++?

Dan Strick strick at covad.net
Thu Apr 20 17:41:29 UTC 2006


Most software is probably written in C ranther than C++ because fewer
programmers are familiar with C++.  Note that this means C++ programs
are in a practical sense less portable and more difficult to maintain
even though the GNU C++ compiler is a good implementation, widely
available and pretty much standard in all unix-like OS distribution.

It may be just as well that most software is written in C.  Programs
written in C probably run only a very few percent slower when compiled
as C++.  (I am assuming the programs are written in the common subset
language.)  Even language features peculiar to C++ are generally
implemented quite efficiently.

Programs written in a good C++ style naturally use C++ standard library
facilities (classes, private functions, templates) that can be expensive.
Since C++ programmers generally do not consider the underlying
implementations (arguably a very good thing), significant unintended
run-time overhead can result.

For example, I once rewrote a program in C++ and discovered the C++
version ran about 5.5 times slower than the original C version.
My "mistake": I used the C++ string and iostream classes.  After
rewriting the C++ program to use C stdio instead of C++ iostreams
and replacing C++ string classes with C character arrays, the C++
program ran only 1.5 times slower than the C program.  I must note
that the C++ version was in some ways a lot cleaner than the C version.

The problem is that a major reason for using C++ in the first place is
to take advantage of these specific C++ library features.  A major
motivation for the development of C++ itself was to facilitate code
sharing by better isolating main program code from library implemention
details.  A C++ program that avoids using simplifying standard library
facilities by reimplementing them is arguably bad C++.
 



More information about the freebsd-chat mailing list