Item 1: View C++ as a federation of languages
Concept
C++ is not a single unified language — it's a federation of four sub-languages, each with its own conventions:
- C — blocks, statements, preprocessor, built-in types, arrays, pointers
- Object-Oriented C++ — classes, encapsulation, inheritance, polymorphism, virtual functions
- Template C++ — generic programming, template metaprogramming (TMP)
- The STL — containers, iterators, algorithms, function objects
Rules for effective programming vary depending on which sub-language you're using. For example, pass-by-value is efficient for built-in C types, but pass-by-reference-to-const is generally preferred in Object-Oriented C++.
Code Example
// Sub-language 1: C
void c_sublanguage() {
int arr[] = {3, 1, 4, 1, 5};
int* p = arr;
}
// Sub-language 2: Object-Oriented C++
class Shape {
public:
virtual ~Shape() = default;
virtual double area() const = 0;
};
// Sub-language 3: Template C++
template <typename Container>
void print_all(const Container& c) { /* ... */ }
// Sub-language 4: The STL
void stl_sublanguage() {
std::vector<int> v = {5, 2, 8, 1, 9, 3};
std::sort(v.begin(), v.end());
}
Things to Remember
- Rules for effective C++ programming vary, depending on the part of C++ you are using.