Chapter 16: History and Compatibility
16.1
C++ was deisgned to provide Simula's facilities for program organization.
16.1.1
Timeline of C++ evoluation
16.1.2
C++ was designed primarily so that my friends and I would not have to program in assembler, C, or various then-fashionable high-level languages.
"new function" and "delete function" were renamed "constructor" and "destructor". Here is the root of C++'s strategies for resource management (causing a demand for expcetions) and the key to many techinques for making user code short and clear.
Note not "C++ is an object-oriented programming language.". Here "supports data abstraction" refers to information hiding, classes that are not part of class hierarchies, and generic programming.
Multiple inheritance of abstract classes (often called interfaces) is now universal in languages supporting static type checking and object-oriented programming.
C++ grew up in an environment with a multitude of established and experimental programming languages. At the time, I was comfortable in about 25 languages, and their influences on C++ are documented in [Stroustrup, 1994] and [Stroustrup, 2007]. However, the determining influences awalys come from the applications I encountered. It was a delibrate policy to have the development of C++ "porblem driven" rather than imitative.
16.1.3
C++17 was meant to be a major release. By "major", I mean containing features that will change the way we think about design and struture our software. By this definition, C++17 was at best a medium release.
16.1.4
A standard says what will work, and how. It does not say what constitues good and effective use. There are significant differences between understanding the technical details of programming language features and using them effectively in combination with other features, libraries, and tools to produce better software.
There are still people who see C++ as a few minor additoins to C and people who consider 1980s Object-Oriented programming styles based on massive class hierarchies the pinnacle of development.
16.1.5
Naturally, the growth rate slowed since that initial growth spurt, but my best estimate is that there are about 4.5 million C++ programmers in 2018. Much of that growth happned after 2005 when the exponential explosion of processor speed stopped so that language performance grew in importance.
C++ is primarily an industrial language; that is, it is more prominent in industry than in education or programming language research.
16.2
Here the book listed C++11/C++14/C++17 language features as well as language components. Can be used as a check list.
16.3
In nothing else, C code fragments can be compiled as C and linked to using the extern C mechanism.
16.3.2.1
A simple line-for-line conversion of a C program into a C++ program yields a program that is often a bit better checked. In fact, I have never converted a C program into C++ without finding some bug. However, the fundamental structure is unchanged, and so are the fundamental sources of errors.
- don't think C++ as C with a few features added.
- Use the C++ standard library as a teach of new techinques and programming styles
- Macro substituion is almost never necessary in C++. Use
const,constexpr,enum,enum class,inline,template,namespace. - Don't declare a variable before you need it and initialize it immediately.
- Don't use
malloc(). Thenewoperator does the same job better, and instead ofrealloc(), try avector. Don't just replacemalloc()andfree()with "naked"newanddelete - Avoid
void*, unions, and casts from C. - If you have to use an explicit type conversion, use an appropriate named cast (e.g.
static_cast) - Minimize the use of arrays and C-style strings.
- Avoid pointer arithmetic except in very specialized code.
- Do not assume that something laboriously written in C style is more efficient than a short alternative.
16.3.2.2
In C, a void* may be used as the right-hand operand of an assignment to or initilization of a variable of any pointer type; in C++ it may not. In both languages, cast the result of malloc() to the right type.
16.3.2.3
To give a C++ functino C linkage (so that it can be called from a C program fragment) or to allow a C function to be called from a C++ program fragment, declar it extern "C"
Now sqrt(double) can be called from a C or a C++ code fragment.