Modern C++ for C Programmers: introduction
2018 is an exciting time to be a programmer, with so many good “full service” programming languages to choose from: C, C++ 2017, Go, Python, Rust and who knows Swift too. Programming languages are complicated beasts - even the simplest languages have specifications that run (or would run) in the hundreds of pages, and once you include everything, no serious language is likely to clock in under a thousand.
With this comes the fact that every programming language has good parts, and frequently even more bad parts. Evangelists tend to talk a lot about the former, and write volumes on the worst parts of other programming languages.
As Bjarne Stroustrup (the inventor of C++) correctly noted “There are only two kinds of languages: the ones people complain about and the ones nobody uses”. Anyone who professes nothing but love for their chosen programming language is likely not being honest. Every language is a set of tradeoffs between speed, simplicity, completeness, expressiveness, safety and probably other aspects.
I’m a big fan of Lua which managed to avoid a ton of complexity by restricting itself severely. What has remained is a powerful programming language that can be interpreted, compiled and embedded with great ease and performance. But it came at a cost - you can really learn Lua in 20 minutes, but that really is all there is too.
At the other end of the spectrum is modern C++ (2017 era) which decided to attempt to be all things to all people: fast, powerful, complete and highly expressive. The cost has been obvious: there is huge complexity if you use all the features.
In this and subsequent posts, I hope to convince C programmers to give ‘2017 era C++’ (which is entirely unlike 2003 C++) another good look. To do so, I want to show that within C++ hides a simple language that still offers you many good things without immediately requiring you to tackle all 1400 pages of ‘The C++ Programming Language’. In other words, I claim there is great benefit already when only using a judicious selection of the best parts of C++.
My goal is that when you go look for a new language to learn (say, Go or Rust), you will hopefully consider modern C++ as well.
On this series
In this series of posts, I target C++ 2014, for which compilers are widely available. An occasional C++ 2017 feature may slip in. This series will touch on immediately useful parts of C++ that C programmers can benefit from without going “whole hog” C++. The goal is to enable developers to benefit from C++ “one line at a time”.
Specifically, I will not cover:
- Multiple inheritance
- Template metaprogramming
- C++ iostreams (except for standard output, for the rest, there is stdio)
- C++ locales (use the C ones)
- User-defined literals
Source code of the snippets shown in this series can be found on GitHub.
Table of contents
- Part 1
- Relation between C and C++
- Why C++ sort is faster than C qsort
- Part 2
- Resource Acquisition Is Initialization
- Smart pointers
- Threads, atomics
- Error handling, exceptions
- Part 3
- Inheritance & Polymorphism
- References & Pointers
- A worked example: high-speed source code indexer
- Part 4
- Improving our source code indexer
- Containers & Algorithms
- Finding & Searching
- Futher standard & non-standard containers
- Part 5
- Memory management
- Copy elision / Return Value Optimization
- Smart Pointers, std::unique_ptr
std::move: transferring objects
- Placement new
- General memory management advice
- Part 6
static_assert: compile time checks
constexpr: compile time execution
numeric_limits: information about floating point and integers
- iostreams: the optional C++ I/O system
- Measuring time
- Scoped or Class Enums
- Regular expressions
- Initializer lists
- Default parameters, function overloading
- Recommended book list