C++ vs. C
C++ vs. C
Compared to C, C++ has a bunch of new language features with little or no runtime overhead because they are translated to code equally as efficient as the equivalent C code:
- function overloading
- STL, the part of the C++ standard library with containers and algorithms
A few features are as efficient as C yet still rawther deceptive and easy to misuse because the "simple" syntax hides how much code is actually being generated:
- operator overloading, when compared to separate functions
virtualmethods, when compared to C function pointer tables
- templates, when compared to using the preprocessor to instantiate multiple copies
C++ also has some features requiring possibly expensive runtime library support:
- the default version of
std::nothrow, which throws exceptions and is used internally by the standard library
Templates have a couple drawbacks:
- more complicated error messages
- programmers who lose track of for how many different type combinations they have instantiated a template, causing code size to balloon
throw) also have a couple drawbacks:
- The size of the required library support might cause a problem on embedded or handheld devices with little RAM.
- C++ has no counterpart to the
finallykeyword of Java and Python. True, there isn't as much need for
finallyin C++ as in languages that rely on a garbage collector, given the idiom of allocating resources in constructors that C++'s deterministic destruction allows. But a method often still needs to restore the object's fields to a consistent state before eating or rethrowing the exception.
Apparently one STL implementation can be built without the use of exceptions: STLPort. I'd be interested to hear about whether it has worked for you.
<iostream> library is another divisive issue.
It was envisioned as a type-safe alternative to
<cstdio>, but implementations are hairy, bloated, inefficient.
Yet some C++ fanboys claim that anything using good old
<cstdio> instead of new-fangled
<iostream> isn't in the spirit of C++, whatever that means. They cling to item 2 in the second edition of Scott Meyers Effective C++ and ignore item 23 of his sequel ("consider alternative libraries"). It appears that Meyers eventually recognized that
<iostream> is imperfect and removed item 2 from the third edition.
- C++ FQA Lite, offering a rebuttal to the fandom's C++ FAQ Lite