discovering_modern_c_plus_plus_2nd_edition_preface

Discovering Modern C++ Preface

Return to Discovering Modern C++, C++ DevOps, C++ books, C++ courses, C++ topics, C++

“The world is built on C++ (and its C subset).” — Herb Sutter

“The infrastructures at Google, Amazon, and Facebook are built using components and services designed and implemented in the C plus plus programming language (implemented in C plus plus). A considerable portion of the technology stack of operating systems, networking equipment, and storage systems is implemented in C++. In telecommunication systems, almost all landline and cellular phone connections are orchestrated by C++ software. And key components in industrial and transportation systems, including automated toll collection systems, and autonomous cars, trucks, and autobuses depend on C++.

In science and engineering, most high-quality software packages today are implemented in C++. The strength of the language is evidenced when projects exceed a certain size and data structures and algorithms become non-trivial. It is no wonder that many—if not most—simulation software programs in computational science are realized today in C++; these include FLUENT, Abaqus, deal.II, FEniCS, OpenFOAM, and G+Smo. Even embedded systems are increasingly realized in C++ thanks to more powerful embedded processors and improved compilers. And the new application domains of the Internet of Things (IoT) and embedded edge intelligence are all driven by C++ platforms such as TensorFlow, Caffe2, and CNTK.

Core services you use every day are based on C++: your cell phone, your car, communication and industrial infrastructure, and key elements in media and entertainment services all contain C++ components. C++ services and applications are omnipresent in modern society. The reason is simple. The C++ language has progressed with its demands, and in many ways leads the innovations in programming productivity and execution efficiency. Both attributes make it the language of choice for applications that need to run at scale.

Reasons to Learn C++ Like no other language, C++ masters the full spectrum from programming sufficiently close to the hardware on one end to abstract high-level programming on the other. The lower-level programming—like user-definable memory management—empowers you as a programmer to understand what really happens during execution, which in turn helps you understand the behavior of programs in other languages. In C++ you can write extremely efficient programs that can only be slightly outperformed by code written in machine language with ridiculous effort. However, you should wait a little with the hardcore performance tuning and focus first on clear and expressive software.

This is where the high-level features of C++ come into play. The language supports a wide variety of programming paradigms directly: object-oriented programming (Chapter 6), generic programming (Chapter 3), meta-programming (Chapter 5), concurrent programming (§4.6), and procedural programming (§1.5), among others.

Several programming techniques—like RAII (§2.4.2.1) and expression templates (§5.3)— were invented in and for C++. As the language is so expressive, it was often possible to establish these new techniques without changing the language. And who knows, maybe one day you will invent a new technique.

Reasons to Read This Book The material in the book has been tested on real humans. The author taught his class, “C++ for Scientists,” over three years (i.e., three times two semesters). The students, mostly from the mathematics department, plus some from the physics and engineering departments, often did not know C++ before the class and were able to implement advanced techniques like expression templates (§5.3) by the end of the course. You can read this book at your own pace: straight to the point by following the main path or more thoroughly by reading additional examples and background information in Appendix A.

The Beauty and the Beast C++ programs can be written in so many ways. In this book, we will lead you smoothly to the more sophisticated styles. This requires the use of advanced features that might be intimidating at first but will become less so once you get used to them. Actually, high-level programming is not only more widely applicable but is usually equally or more efficient and readable.

We will give you a first impression with a simple example: gradient descent with constant step size. The principle is extremely simple: we compute the steepest descent of f(x) with its gradient, say g(x), and follow this direction with fixed-size steps to the next local minimum. Even the algorithmic pseudo-code is as simple as this description:

Algorithm 1: Gradient descent algorithm

 Input: Start value x, step size s, termination criterion ɛ, function f, gradient g
 Output: Local minimum x

1 do

2 | x = x – s · g(x)

3 while |Δf(x)| ≽ ɛ ;

For this simple algorithm, we wrote two quite different implementations. Please have a look and let it sink in without trying to understand the technical details.

Click here to view code image

void gradient_descent(double* x,

   double* y, double s, double eps,
   double(*f)(double, double),
   double(*gx)(double, double),
   double(*gy)(double, double))
{
   double val= f(*x, *y), delta;
   do {
       *x-= s * gx(*x, *y);
       *y-= s * gy(*x, *y);
       double new_val= f(*x, *y);
       delta= abs(new_val - val);
       val= new_val;
   } while (delta > eps);
} template <typename Value, typename P1,
         typename P2, typename F,
         typename G>
Value gradient_descent(Value x, P1 s,
   P2 eps, F f, G g)
{
   auto val= f(x), delta= val;
   do {
       x-= s * g(x);
       auto new_val= f(x);
       delta= abs(new_val - val);
       val= new_val;
   } while (delta > eps);
   return x;
} At first glance, they look pretty similar, and we will tell you soon which one we like more. The first version is in principle pure C, i.e., compilable with a C compiler too. The benefit is that what is optimized is directly visible: a 2D function with double values (indicated by the highlighted function parameters). We prefer the second version as it is more widely usable: two functions of arbitrary dimension with arbitrary value types (visible by the marked type and function parameters). Surprisingly, the versatile implementation is not less efficient. To the contrary, the functions given for F and G may be inlined (see §1.5.3) so that the function call overhead is saved, whereas the explicit use of (ugly) function pointers in the first version makes this code acceleration difficult for the compiler.

A longer example comparing old and new styles is found in Appendix A (§A.1) for the really patient reader. There, the benefit of modern programming is much more evident than in the toy example here. But we do not want to hold you back too long with preliminary skirmishing.

Languages in Science and Engineering “It would be nice if every kind of numeric software could be written in C++ without loss of efficiency, but unless something can be found that achieves this without compromising the C++-type system it may be preferable to rely on Fortran, assembler or architecture-specific extensions.”

—Bjarne Stroustrup

Scientific and engineering software is written in different languages, and which one is the most appropriate depends on the goals and available resources:

Math tools like MATLAB, Mathematica, or R are excellent when we can use their existing algorithms. When we implement our own algorithms with fine-grained (e.g., scalar) operations, we will experience a significant decrease in performance. This might not be an issue when the problems are small or the user is infinitely patient; otherwise we should consider alternative languages.

Python is excellent for rapid software development and already contains scientific libraries like “scipy” and “numpy,” and applications based on these libraries (often implemented in C and C++) are reasonably efficient. Again, user-defined algorithms from fine-grained operations pay a performance penalty. Python is an excellent way to implement small and medium-sized tasks efficiently. When projects grow sufficiently large, it becomes increasingly important that the compiler is stricter (e.g., assignments are rejected when the argument types do not match).

Fortran is also great when we can rely on existing, well-tuned operations like dense matrix operations. It is well suited to accomplishing old professors’ homework (because they only ask for what is easy in Fortran). Introducing new data structures is, in the author’s experience, quite cumbersome, and writing a large simulation program in Fortran is quite a challenge—today only done voluntarily by a shrinking minority.

C allows for good performance, and a large amount of software is written in C. The core language is relatively small and easy to learn. The challenge is to write large and bug-free software with the simple and dangerous language features, especially pointers (§1.8.2) and macros (§1.9.2.1). The last standard was released in 2017 and thus is named C17. Most C features—but not all—were sooner or later introduced into C++.

Languages like Java, C#, and PHP are probably good choices when the main component of the application is a web or graphic interface and not too many calculations are performed.

C++ shines particularly when we develop large, high-quality software with good performance. Nonetheless, the development process does not need to be slow and painful. With the right abstractions at hand, we can write C++ programs quite rapidly. We are optimistic that in future C++ standards, more scientific libraries will be included.

Evidently, the more languages we know, the more choice we have. Moreover, the better we know those languages, the more educated our choice will be. In addition, large projects often contain components in different languages, whereas in most cases at least the performance-critical kernels are realized in C or C++. All this said, learning C++ is an intriguing journey, and having a deep understanding of it will make you a great programmer in any case.

Typographical Conventions New terms are set in clear blue and italic. C++ sources are printed blue and monospace. Important details are marked in boldface. Classes, functions, variables, and constants are lowercase, optionally containing underscores. An exception is matrices, which are usually named with a single capital letter. Template parameters and concepts start with a capital letter and may contain further capitals (CamelCase). Program output and commands are in light blue typewriter font.

Programs requiring C++11, C++14, C++17, or C++20 features are marked with corresponding margin boxes. Several programs making light use of a C++11 feature that is easily substituted by a C++03 expression are not explicitly marked.

Except for very short code illustrations, all programming examples in this book were tested on at least one compiler and in most cases on three compilers: g++, clang++, and Visual Studio. All examples are kept as short as possible for easier understanding. To this end, we don’t incorporate all features that would have been used in comparable production code. Obviously, we minimize the usage of features not introduced yet. It is probably a good idea to review the examples after finishing reading the book and ask yourself what you would code differently based on all the new knowledge you acquired.

For the C++20 examples, we recommend that you try them to see whether they work on your system. Most of the new features weren’t fully supported by all compilers at the time of this writing. Even the existing compiler support might not have been 100 percent correct at that time. For some new libraries, we used prototype implementations when no standard version was available on any compiler (e.g., format library).

⇒ directory/source_code.cpp

The location of the program example relevant to the discussed topic is indicated by an arrow and the paths of the complete programs at the beginning of the paragraph or section. All programs are available on GitHub in the public repository https://github.com/petergottschling/dmc3 and can thus be cloned by:

Click here to view code image

git clone https://github.com/petergottschling/dmc3.git On Windows, it might be more convenient to use TortoiseGit; see tortoisegit.org.

Fair Use Sources

C++: C++ Fundamentals, C++ Inventor - C++ Language Designer: Bjarne Stroustrup in 1985; C++ Keywords, C++ Built-In Data Types, C++ Data Structures (CPP Containers) - C++ Algorithms, C++ Syntax, C++ OOP - C++ Design Patterns, Clean C++ - C++ Style Guide, C++ Best Practices ( C++ Core Guidelines (CG)) - C++ BDD, C++ Standards ( C++ 23, C++ 20, C++ 17, C++ 14, C++ 11, C++ 03, C++ 98), Bjarne Stroustrup's C++ Glossary, CppReference.com, CPlusPlus.com, ISOcpp.org, C++ Compilers (Compiler Explorer, MinGW), C++ IDEs, C++ Development Tools, C++ Linter, C++ Debugging, C++ Modules ( C++20), C++ Packages, C++ Package Manager ( Conan - the C/C++ Package Manager), C++ Standard Library, C++ Libraries, C++ Frameworks, C++ DevOps - C++ SRE, C++ CI/CD ( C++ Build Pipeline), C++ Data Science - C++ DataOps, C++ Machine Learning, C++ Deep Learning, Functional C++, C++ Concurrency, C++ History, C++ Topics, C++ Bibliography, Manning C++ Series, C++ Courses, CppCon, C++ Research, C++ GitHub, Written in C++, C++ Popularity, C++ Awesome , C++ Versions. (navbar_cplusplus – see also navbar_cpp_containers, navbar_cppcon, navbar_cpp_core_guidelines, navbar_cpp23, navbar_cpp20, navbar_cpp17, navbar_cpp14, navbar_cpp11)


Cloud Monk is Retired (for now). Buddha with you. © 2005 - 2024 Losang Jinpa or Fair Use. Disclaimers

SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.


discovering_modern_c_plus_plus_2nd_edition_preface.txt · Last modified: 2022/02/28 13:21 by 127.0.0.1